forked from mystiq/hydrogen-web
throw NetworkError from HomeServerApi
This commit is contained in:
parent
f3034800ae
commit
049e70e312
2 changed files with 18 additions and 7 deletions
|
@ -9,5 +9,7 @@ export class StorageError extends Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class RequestAbortError extends Error {
|
export class RequestAbortError extends Error {
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NetworkError extends Error {
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import {
|
import {
|
||||||
HomeServerError,
|
HomeServerError,
|
||||||
RequestAbortError
|
RequestAbortError,
|
||||||
|
NetworkError
|
||||||
} from "./error.js";
|
} from "./error.js";
|
||||||
|
|
||||||
class RequestWrapper {
|
class RequestWrapper {
|
||||||
|
@ -62,10 +63,18 @@ export default class HomeServerApi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, err => {
|
}, err => {
|
||||||
switch (err.name) {
|
if (err.name === "AbortError") {
|
||||||
case "AbortError": throw new RequestAbortError();
|
throw new RequestAbortError();
|
||||||
default: throw err; //new Error(`Unrecognized DOMException: ${err.name}`);
|
} 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);
|
return new RequestWrapper(promise, controller);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue