access: Fix error handling in JS
This commit is contained in:
@ -129,7 +129,7 @@ function render() {
|
||||
main.className = 'overview';
|
||||
main.innerHTML = `
|
||||
<h1>${CLIENTS[client].name}</h1>`;
|
||||
updateOverview(client).then();
|
||||
update();
|
||||
} else {
|
||||
window.location.hash = `#/${client}`;
|
||||
}
|
||||
@ -142,7 +142,17 @@ function update() {
|
||||
// do nothing
|
||||
} else {
|
||||
if (hash === `#/${client}`) {
|
||||
updateOverview(client).then();
|
||||
updateOverview(client)
|
||||
.then()
|
||||
.catch(e => {
|
||||
if (e instanceof ApiError && e.statusCode === 401) {
|
||||
window.localStorage.removeItem(`${CLIENT}/${client}/token`);
|
||||
window.localStorage.removeItem(`${CLIENT}/${client}/password`);
|
||||
window.location.hash = `#/${client}/login`;
|
||||
} else {
|
||||
alert(e.localizedMessage ?? ERROR_MESSAGES[e.message] ?? `Unbekannter Fehler: ${e.message}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -172,7 +182,7 @@ function actionLogin(form) {
|
||||
}).catch(e => {
|
||||
const error = document.createElement('div');
|
||||
error.className = 'error';
|
||||
error.innerText = e.localizedMessage ?? ERROR_MESSAGES[e.message] ?? 'Unbekannter Fehler';
|
||||
error.innerText = e.localizedMessage ?? ERROR_MESSAGES[e.message] ?? `Unbekannter Fehler\n(${e.message})`;
|
||||
form.insertBefore(error, form.lastChild.previousSibling);
|
||||
});
|
||||
|
||||
|
@ -8,6 +8,7 @@ const ERROR_MESSAGES = {
|
||||
class ApiError extends Error {
|
||||
constructor(statusCode, message) {
|
||||
super(statusCode + ' - ' + message);
|
||||
this.statusCode = statusCode;
|
||||
this.name = 'ApiError';
|
||||
this.localizedMessage = ERROR_MESSAGES[statusCode];
|
||||
}
|
||||
|
Reference in New Issue
Block a user