kopia lustrzana https://gitlab.com/rysiekpl/libresilient
tests passing for work done so far for error handling improvements (ref. #36)
rodzic
cf0b9d93b0
commit
b7419d0fe8
|
@ -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);
|
||||
});
|
||||
})
|
||||
|
|
|
@ -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'
|
||||
)
|
||||
});
|
||||
|
||||
})
|
||||
|
|
|
@ -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:'
|
||||
)
|
||||
});
|
||||
})
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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'
|
||||
)
|
||||
});
|
||||
|
||||
})
|
||||
|
|
|
@ -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'
|
||||
)
|
||||
});
|
||||
|
||||
})
|
||||
|
|
|
@ -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
|
||||
|
|
Ładowanie…
Reference in New Issue