diff --git a/src/matrix/error.js b/src/matrix/error.js index 83cc8652..520698d9 100644 --- a/src/matrix/error.js +++ b/src/matrix/error.js @@ -9,5 +9,7 @@ export class StorageError extends Error { } export class RequestAbortError extends Error { +} -} \ No newline at end of file +export class NetworkError extends Error { +} diff --git a/src/matrix/hs-api.js b/src/matrix/hs-api.js index 2eb2a1f1..e54b66f6 100644 --- a/src/matrix/hs-api.js +++ b/src/matrix/hs-api.js @@ -1,6 +1,7 @@ import { HomeServerError, - RequestAbortError + RequestAbortError, + NetworkError } from "./error.js"; class RequestWrapper { @@ -62,10 +63,18 @@ export default class HomeServerApi { } } }, err => { - switch (err.name) { - case "AbortError": throw new RequestAbortError(); - default: throw err; //new Error(`Unrecognized DOMException: ${err.name}`); - } + if (err.name === "AbortError") { + throw new RequestAbortError(); + } else if (err instanceof TypeError) { + // Network errors are reported as TypeErrors, see + // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful + // this can either mean user is offline, server is offline, or a CORS error (server misconfiguration). + // + // One could check navigator.onLine to rule out the first + // but the 2 later ones are indistinguishable from javascript. + throw new NetworkError(err.message); + } + throw err; }); return new RequestWrapper(promise, controller); } @@ -92,4 +101,4 @@ export default class HomeServerApi { "password": password }); } -} \ No newline at end of file +}