handling 5xx HTTP errors directly in the service worker (ref. #36)

master^2
Michał 'rysiek' Woźniak 2024-03-01 18:23:23 +00:00
rodzic 18e436568a
commit 933bcbe7b4
2 zmienionych plików z 28 dodań i 9 usunięć

Wyświetl plik

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

Wyświetl plik

@ -1045,7 +1045,18 @@ let libresilientFetch = (plugin, url, init, reqInfo) => {
// starting the fetch... // starting the fetch...
// if it errors out immediately, at least we don't have to deal // if it errors out immediately, at least we don't have to deal
// with a dangling promise timeout, set up below // with a dangling promise timeout, set up below
let fetch_promise = plugin.fetch(url, init) let fetch_promise = plugin
.fetch(url, init)
.then((response)=>{
// 5xx? that's a paddlin'
// we do want to pass 3xx and 4xx on back to the client though!
if (response.status >= 500) {
// throw an Error to fall back to LibResilient:
throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText);
}
// ok, we're good
return response
})
let timeout_promise, timeout_id let timeout_promise, timeout_id
[timeout_promise, timeout_id] = promiseTimeout( [timeout_promise, timeout_id] = promiseTimeout(
@ -1256,7 +1267,7 @@ let getResourceThroughLibResilient = (url, init, clientId, useStashed=true, doSt
}) })
// a final catch... in case all plugins fail // a final catch... in case all plugins fail
.catch((err)=>{ .catch((err)=>{
self.log('service-worker', "LibResilient failed completely: ", err, self.log('service-worker', "all plugins failed: ", err,
'\n+-- URL : ' + url) '\n+-- URL : ' + url)
// cleanup // cleanup
@ -1264,7 +1275,17 @@ let getResourceThroughLibResilient = (url, init, clientId, useStashed=true, doSt
state: "failed", state: "failed",
error: err error: err
}) })
decrementActiveFetches(clientId) decrementActiveFetches(clientId)
// print out all the errors from plugins in console for debugging purposes
self.log('service-worker', `request errored out:\n+-- url: ${reqInfo.url}\n+-- plugin errors:\n${
reqInfo
.errors
.reduce((acc, cur)=>{
return acc + ' ' + cur.join(': ') + '\n'
}, '')}`)
// rethrow // rethrow
throw err throw err
}) })