LibResilientResourceInfo can now hold errors for all plugins (ref. #36)

master^2
Michał 'rysiek' Woźniak 2024-02-28 06:33:37 +00:00
rodzic 17d1aee6db
commit 81dfaab7c5
1 zmienionych plików z 24 dodań i 15 usunięć

Wyświetl plik

@ -883,12 +883,14 @@ let LibResilientResourceInfo = class {
this.values = {
url: '', // read only after initialization
clientId: null, // the client on whose behalf that request is being processed
lastError: null, // error from the previous plugin (for state:running) or the last emitted error (for state:failed or state:success)
method: null, // name of the current plugin (in case of state:running) or last plugin (for state:failed or state:success)
state: null, // can be "failed", "success", "running"
serviceWorker: 'COMMIT_UNKNOWN' // this will be replaced by commit sha in CI/CD; read-only
}
// errors from the plugins, contains tuples: [plugin-name, exception-or-response-object]
this.errors = []
// queued messages for when we have a client available
this.messageQueue = []
@ -915,11 +917,11 @@ let LibResilientResourceInfo = class {
var msg = 'Updated LibResilientResourceInfo for: ' + this.values.url
// was there a change? if not, no need to postMessage
var changed = false
// update the properties that are read-write
// update simple read-write properties
Object
.keys(data)
.filter((k)=>{
return ['lastError', 'method', 'state'].includes(k)
return ['method', 'state'].includes(k)
})
.forEach((k)=>{
msg += '\n+-- ' + k + ': ' + data[k]
@ -929,23 +931,31 @@ let LibResilientResourceInfo = class {
}
this.values[k] = data[k]
})
// start preparing the data to postMessage() over to the client
let msgdata = {...this.values}
// handle any error related info
if ('error' in data) {
// push the error info, along with method that generated it, onto the error stack
this.errors.push([this.values.method, data.error])
// response?
if ("statusText" in data.error) {
msgdata.error = `HTTP status: ${data.error.status} ${data.error.statusText}`
// nope, exception
} else {
msgdata.error = data.error.toString()
}
changed = true
}
self.log('service-worker', msg)
// send the message to the client
if (changed) {
postMessage(
this.values.clientId,
{...this.values}
msgdata
);
}
}
/**
* lastError property
*/
get lastError() {
return this.values.lastError
}
/**
* method property
*/
@ -1129,7 +1139,7 @@ let getResourceThroughLibResilient = (url, init, clientId, useStashed=true, doSt
'\n+-- error : ' + error.toString())
// save info in reqInfo -- status of the previous method
reqInfo.update({
lastError: error.toString()
error: error
})
return libresilientFetch(currentPlugin, url, init, reqInfo)
})
@ -1146,7 +1156,6 @@ let getResourceThroughLibResilient = (url, init, clientId, useStashed=true, doSt
// record the success
reqInfo.update({
lastError: null,
state:"success"
})
@ -1246,7 +1255,7 @@ let getResourceThroughLibResilient = (url, init, clientId, useStashed=true, doSt
// cleanup
reqInfo.update({
state: "failed",
lastError: err.toString()
error: err
})
decrementActiveFetches(clientId)
// rethrow
@ -1469,7 +1478,7 @@ self.addEventListener('fetch', async event => {
header.innerHTML = "Loading failed."
text.innerHTML = "We're sorry, we were unable to load this page."
}
if ( ( 'lastError' in event.data ) && ( typeof event.data.lastError === 'string' ) ) {
if ( ( 'error' in event.data ) && ( typeof event.data.error === 'object' ) ) {
attempts += 1;
status.innerHTML = attempts;
}