access: Fix error handling in JS

This commit is contained in:
2025-05-03 11:53:19 +02:00
parent 7cf3e21efc
commit 4962218fe3
2 changed files with 14 additions and 3 deletions

View File

@ -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);
});

View File

@ -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];
}