make client init failures spit out valuable error messages

This commit is contained in:
ari melody 2024-06-21 03:02:18 +01:00
parent ffecd51fd2
commit 893ee804c1
Signed by: ari
GPG key ID: CF99829C92678188
2 changed files with 22 additions and 12 deletions

View file

@ -18,14 +18,8 @@
event.preventDefault(); event.preventDefault();
const host = event.target.host.value; const host = event.target.host.value;
client.init(host).then(() => { client.init(host).then(res => {
if (client.instance.type === server_types.INCOMPATIBLE) { if (!res) return;
console.error("Server " + client.instance.host + " is not supported - " + client.instance.version);
alert("Sorry, this app is not compatible with " + client.instance.host + "!");
return;
}
console.log("Server is \"" + client.instance.type + "\" (or compatible).");
client.save();
let oauth_url = client.getOAuthUrl(); let oauth_url = client.getOAuthUrl();
location = oauth_url; location = oauth_url;
}); });

View file

@ -40,14 +40,20 @@ export class Client {
client = new Client(); client = new Client();
window.peekie = client; window.peekie = client;
client.load(); client.load();
if (client.instance) client.#configureAPI(); if (client.instance && client.instance !== server_types.INCOMPATIBLE)
client.#configureAPI();
return client; return client;
} }
async init(host) { async init(host) {
if (host.startsWith("https://")) host = host.substring(8); if (host.startsWith("https://")) host = host.substring(8);
const url = "https://" + host + "/api/v1/instance"; const url = `https://${host}/api/v1/instance`;
const data = await fetch(url).then(res => res.json()); const data = await fetch(url).then(res => res.json()).catch(error => { console.log(error) });
if (!data) {
console.error(`Failed to connect to ${host}`);
alert(`Failed to connect to ${host}! Please try again later.`);
return false;
}
this.instance = { this.instance = {
host: host, host: host,
version: data.version, version: data.version,
@ -62,6 +68,14 @@ export class Client {
} }
} }
if (this.instance.type == server_types.INCOMPATIBLE) {
console.error(`Server ${host} is not supported - ${data.version}`);
alert(`Sorry, this app is not compatible with ${host}!`);
return false;
}
console.log(`Server is "${client.instance.type}" (or compatible).`);
this.#configureAPI(); this.#configureAPI();
this.app = await this.api.createApp(host); this.app = await this.api.createApp(host);
@ -70,7 +84,9 @@ export class Client {
return false; return false;
} }
return this.auth; this.save();
return true;
} }
#configureAPI() { #configureAPI() {