tinypilot
tinypilot copied to clipboard
Handle network error gracefully when waiting for shutdown/restart (Chrome)
In controller.js in the shutdown() we have:
fetch(/*........*/)
/* ....... */
.catch((error) => {
// Depending on timing, the server may not respond to the shutdown
// request because it's shutting down. If we get a NetworkError, assume
// the shutdown succeeded.
if (error.message.indexOf("NetworkError") >= 0) {
return Promise.resolve({});
}
return Promise.reject(error);
});
This special handling of network failures only works in Firefox. The error messages are not standardized, so in Chrome e.g. it doesn’t contain NetworkError, so that clause doesn’t come into effect there.
Instead, fetch only ever rejects on network errors, but never on status codes, ~so it should be fine to omit that if block.~. The if block can’t be just omitted, though, because the preceding then might also throw, so we need to distinguish these failures elsewise somehow.
Stumbled across this while working on the controllers. Happy to fix later some time, just wanted to put it here to track so that we don’t forget.
Oh hang on, it’s actually not that simple: there is a then block before which also might throw errors. They would be swallowed then, too.