Fixed error handling

This commit is contained in:
2021-06-05 15:05:03 +02:00
parent 5b2deaa346
commit 0217ea90e2
2 changed files with 40 additions and 22 deletions

View File

@ -15,11 +15,11 @@ export class App {
this.session = null;
const main = document.getElementsByTagName("main")[0];
if (!main) throw Error("Element <main> not found");
if (!main) throw new Error("Element <main> not found");
this.main = main;
const windows = document.getElementById("windows");
if (!windows) throw Error("Element #windows not found");
if (!windows) throw new Error("Element #windows not found");
this.windows = windows;
window.addEventListener("hashchange", event => {
@ -84,9 +84,9 @@ export class App {
domain = await USIMP.Domain.fromName(domainName);
} catch (error) {
if (error instanceof DOMException) {
throw Error("Connection timed out");
throw new Error("Connection timed out");
} else {
throw Error("Invalid USIMP domain");
throw new Error("Invalid USIMP domain");
}
}
@ -101,7 +101,7 @@ export class App {
this.setHash("/");
} else {
console.error(response.error);
throw Error(response.error.message || response.error.code);
throw new Error(response.error.message || response.error.code);
}
}
@ -143,7 +143,7 @@ export class App {
msg.innerText = message;
const chat = this.main.getElementsByClassName("chat-history")[0];
if (!chat) throw Error("Element .chat-history not found");
if (!chat) throw new Error("Element .chat-history not found");
chat.appendChild(msg);
chat.scrollTop = chat.scrollHeight;
@ -163,7 +163,7 @@ export class App {
</form>`;
const form = win.getElementsByTagName("form")[0];
if (!form) throw Error("Element <form> not found");
if (!form) throw new Error("Element <form> not found");
form.addEventListener("submit", async event => {
event.preventDefault();
@ -201,14 +201,14 @@ export class App {
this.windows.appendChild(win);
const domain = document.getElementsByName("domain")[0];
if (!domain) throw Error("Element name=domain not found");
if (!domain) throw new Error("Element name=domain not found");
domain.addEventListener("input", (event) => {
domain.removeAttribute("invalid");
});
const account = document.getElementsByName("account")[0];
if (!account) throw Error("Element name=account not found");
if (!account) throw new Error("Element name=account not found");
account.addEventListener("input", (event) => {
account.removeAttribute("invalid");
@ -218,7 +218,7 @@ export class App {
}
setupMain() {
if (!this.session) throw Error("Invalid state");
if (!this.session) throw new Error("Invalid state");
this.main.innerHTML = `
<div class="chat">
@ -229,11 +229,11 @@ export class App {
</div>`;
const input = document.getElementsByTagName("input")[0];
if (!input) throw Error("Element <input> not found");
if (!input) throw new Error("Element <input> not found");
input.addEventListener("keyup", async event => {
if (event.key === "Enter" && input.value.length > 0) {
if (!this.session) throw Error("No session found");
if (!this.session) throw new Error("No session found");
this.addMessage(input.value);
const val = input.value;