tests passing for work done so far for error handling improvements (ref. #36)

master^2
Michał 'rysiek' Woźniak 2024-03-06 16:33:22 +00:00
rodzic cf0b9d93b0
commit b7419d0fe8
7 zmienionych plików z 6 dodań i 165 usunięć

Wyświetl plik

@ -122,32 +122,4 @@ describe('browser: any-of plugin', async () => {
})
assertEquals(await response.json(), {test: "success"})
});
it("should throw an error when HTTP status is >= 400", async () => {
window.fetch = spy((url, init) => {
return Promise.resolve(
new Response(
new Blob(
["Not Found"],
{type: "text/plain"}
),
{
status: 404,
statusText: "Not Found"
}
)
)
});
assertRejects(
async () => {
return await LibResilientPluginConstructors
.get('any-of')(LR, init)
.fetch('https://resilient.is/test.json') },
AggregateError,
'All promises were rejected'
)
assertSpyCalls(fetch, 1);
});
})

Wyświetl plik

@ -99,28 +99,4 @@ describe('browser: fetch plugin', async () => {
assertEquals(response.headers.get('X-LibResilient-ETag'), 'TestingETagHeader')
});
it("should throw an error when HTTP status is >= 400", async () => {
window.fetch = (url, init) => {
const response = new Response(
new Blob(
["Not Found"],
{type: "text/plain"}
),
{
status: 404,
statusText: "Not Found",
url: url
});
return Promise.resolve(response);
}
assertRejects(
async () => {
return await LibResilientPluginConstructors
.get('fetch')(LR)
.fetch('https://resilient.is/test.json') },
Error,
'HTTP Error: 404 Not Found'
)
});
})

Wyświetl plik

@ -1950,58 +1950,4 @@ describe('browser: dnslink-fetch plugin', async () => {
assertEquals(response.headers.get('X-LibResilient-Method'), 'dnslink-fetch')
assertEquals(response.headers.get('X-LibResilient-Etag'), 'TestingLastModifiedHeader')
});
it("should throw an error when HTTP status is >= 400", async () => {
window.resolvingFetch = (url, init) => {
if (url.startsWith('https://dns.hostux.net/dns-query')) {
const response = new Response(
new Blob(
[JSON.stringify(fetchResponse[0])],
{type: fetchResponse[1]}
),
{
status: 200,
statusText: "OK",
headers: {
'Last-Modified': 'TestingLastModifiedHeader'
},
url: url
});
return Promise.resolve(response);
} else {
const response = new Response(
new Blob(
["Not Found"],
{type: "text/plain"}
),
{
status: 404,
statusText: "Not Found",
url: url
});
return Promise.resolve(response);
}
}
window.fetch = spy(window.resolvingFetch)
window.fetchResponse = [
{Status: 0, Answer: [
{type: 16, data: 'dnslink=/https/example.org'},
{type: 16, data: 'dnslink=/http/example.net/some/path'}
]},
"application/json"
]
assertRejects(
async ()=>{
const response = await LibResilientPluginConstructors
.get('dnslink-fetch')(LR, init)
.fetch('https://resilient.is/test.json')
console.log(response)
},
Error,
'HTTP Error:'
)
});
})

Wyświetl plik

@ -226,11 +226,7 @@
u=>fetch(u, init)
))
.then((response) => {
// 4xx? 5xx? that's a paddlin'
if (response.status >= 400) {
// throw an Error to fall back to other plugins:
throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText);
}
// all good, it seems
LR.log(pluginName, "fetched:", response.url);

Wyświetl plik

@ -99,28 +99,4 @@ describe('browser: fetch plugin', async () => {
assertEquals(response.headers.get('X-LibResilient-ETag'), 'TestingETagHeader')
});
it("should throw an error when HTTP status is >= 400", async () => {
window.fetch = (url, init) => {
const response = new Response(
new Blob(
["Not Found"],
{type: "text/plain"}
),
{
status: 404,
statusText: "Not Found",
url: url
});
return Promise.resolve(response);
}
assertRejects(
async () => {
return await LibResilientPluginConstructors
.get('fetch')(LR)
.fetch('https://resilient.is/test.json') },
Error,
'HTTP Error: 404 Not Found'
)
});
})

Wyświetl plik

@ -99,28 +99,4 @@ describe('browser: fetch plugin', async () => {
assertEquals(response.headers.get('X-LibResilient-ETag'), 'TestingETagHeader')
});
it("should throw an error when HTTP status is >= 400", async () => {
window.fetch = (url, init) => {
const response = new Response(
new Blob(
["Not Found"],
{type: "text/plain"}
),
{
status: 404,
statusText: "Not Found",
url: url
});
return Promise.resolve(response);
}
assertRejects(
async () => {
return await LibResilientPluginConstructors
.get('fetch')(LR)
.fetch('https://resilient.is/test.json') },
Error,
'HTTP Error: 404 Not Found'
)
});
})

Wyświetl plik

@ -32,12 +32,11 @@
// run built-in regular fetch()
return fetch(url, init)
.then(async (response) => {
// 4xx? 5xx? that's a paddlin'
if (response.status >= 400) {
// throw an Error to fall back to LibResilient:
throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText);
}
// all good, it seems
// we got something, it seems
// it might be a 2xx; it might be a 3xx redirect
// it might also be a 4xx or a 5xx error
// the service worker will know how to deal with those
LR.log(pluginName, `fetched successfully: ${response.url}`);
// we need to create a new Response object