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 = { this.values = {
url: '', // read only after initialization url: '', // read only after initialization
clientId: null, // the client on whose behalf that request is being processed 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) 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" state: null, // can be "failed", "success", "running"
serviceWorker: 'COMMIT_UNKNOWN' // this will be replaced by commit sha in CI/CD; read-only 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 // queued messages for when we have a client available
this.messageQueue = [] this.messageQueue = []
@ -915,11 +917,11 @@ let LibResilientResourceInfo = class {
var msg = 'Updated LibResilientResourceInfo for: ' + this.values.url var msg = 'Updated LibResilientResourceInfo for: ' + this.values.url
// was there a change? if not, no need to postMessage // was there a change? if not, no need to postMessage
var changed = false var changed = false
// update the properties that are read-write // update simple read-write properties
Object Object
.keys(data) .keys(data)
.filter((k)=>{ .filter((k)=>{
return ['lastError', 'method', 'state'].includes(k) return ['method', 'state'].includes(k)
}) })
.forEach((k)=>{ .forEach((k)=>{
msg += '\n+-- ' + k + ': ' + data[k] msg += '\n+-- ' + k + ': ' + data[k]
@ -929,23 +931,31 @@ let LibResilientResourceInfo = class {
} }
this.values[k] = data[k] 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) self.log('service-worker', msg)
// send the message to the client // send the message to the client
if (changed) { if (changed) {
postMessage( postMessage(
this.values.clientId, this.values.clientId,
{...this.values} msgdata
); );
} }
} }
/**
* lastError property
*/
get lastError() {
return this.values.lastError
}
/** /**
* method property * method property
*/ */
@ -1129,7 +1139,7 @@ let getResourceThroughLibResilient = (url, init, clientId, useStashed=true, doSt
'\n+-- error : ' + error.toString()) '\n+-- error : ' + error.toString())
// save info in reqInfo -- status of the previous method // save info in reqInfo -- status of the previous method
reqInfo.update({ reqInfo.update({
lastError: error.toString() error: error
}) })
return libresilientFetch(currentPlugin, url, init, reqInfo) return libresilientFetch(currentPlugin, url, init, reqInfo)
}) })
@ -1146,7 +1156,6 @@ let getResourceThroughLibResilient = (url, init, clientId, useStashed=true, doSt
// record the success // record the success
reqInfo.update({ reqInfo.update({
lastError: null,
state:"success" state:"success"
}) })
@ -1246,7 +1255,7 @@ let getResourceThroughLibResilient = (url, init, clientId, useStashed=true, doSt
// cleanup // cleanup
reqInfo.update({ reqInfo.update({
state: "failed", state: "failed",
lastError: err.toString() error: err
}) })
decrementActiveFetches(clientId) decrementActiveFetches(clientId)
// rethrow // rethrow
@ -1469,7 +1478,7 @@ self.addEventListener('fetch', async event => {
header.innerHTML = "Loading failed." header.innerHTML = "Loading failed."
text.innerHTML = "We're sorry, we were unable to load this page." 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; attempts += 1;
status.innerHTML = attempts; status.innerHTML = attempts;
} }