move *_user_error from advent22.ts to store.ts, fixing advent22Store.init

This commit is contained in:
Jörn-Michael Miehe 2024-04-03 18:29:08 +00:00
parent 98ab638762
commit c6e61d5d76
5 changed files with 66 additions and 64 deletions

View file

@ -44,7 +44,7 @@ export default class extends Vue {
this.store
.login(creds)
.catch(this.$advent22.alert_user_error)
.catch(this.store.alert_user_error)
.finally(() => (this.is_busy = false));
}

View file

@ -109,7 +109,7 @@ export default class extends Vue {
this.multi_modal!.show_image(image_src, this.$advent22.name_door(day));
})
.catch((error) => {
this.$advent22.alert_user_error(error);
this.store.alert_user_error(error);
this.multi_modal!.hide();
});
}

View file

@ -71,6 +71,7 @@
<script lang="ts">
import { DoorSaved } from "@/lib/api";
import { Door } from "@/lib/door";
import { advent22Store } from "@/plugins/store";
import { Options, Vue } from "vue-class-component";
import { toast } from "bulma-toast";
@ -99,6 +100,7 @@ export default class extends Vue {
];
public current_step = 0;
public doors: Door[] = [];
private readonly store = advent22Store();
public loading_doors = false;
public saving_doors = false;
@ -117,7 +119,7 @@ export default class extends Vue {
resolve();
})
.catch((error) => {
this.$advent22.alert_user_error(error);
this.store.alert_user_error(error);
reject();
});
});
@ -135,7 +137,7 @@ export default class extends Vue {
.api_put("admin/doors", data)
.then(resolve)
.catch((error) => {
this.$advent22.alert_user_error(error);
this.store.alert_user_error(error);
reject();
});
});

View file

@ -1,5 +1,4 @@
import axios, { AxiosError, AxiosInstance, ResponseType } from "axios";
import { toast } from "bulma-toast";
import axios, { AxiosInstance, ResponseType } from "axios";
import { App, Plugin } from "vue";
import { advent22Store } from "./store";
@ -29,62 +28,6 @@ export class Advent22 {
return `Türchen ${day}`;
}
public format_user_error([reason, endpoint]: [unknown, string]): string {
let msg =
"Unbekannter Fehler, bitte wiederholen! Besteht das Problem länger, bitte Admin benachrichtigen!";
let code = "U";
const result = () => `${msg} (Fehlercode: ${code}/${endpoint})`;
if (!(reason instanceof AxiosError)) return result();
switch (reason.code) {
case "ECONNABORTED":
// API unerreichbar
msg =
"API antwortet nicht, bitte später wiederholen! Besteht das Problem länger, bitte Admin benachrichtigen!";
code = "D";
break;
case "ERR_NETWORK":
// Netzwerk nicht verbunden
msg = "Sieht aus, als sei deine Netzwerkverbindung gestört.";
code = "N";
break;
default:
if (reason.response === undefined) return result();
switch (reason.response.status) {
case 401:
// UNAUTHORIZED
msg = "Netter Versuch :)";
code = "A";
break;
case 422:
// UNPROCESSABLE ENTITY
msg = "Funktion ist kaputt, bitte Admin benachrichtigen!";
code = "I";
break;
default:
// HTTP
code = `H${reason.response.status}`;
break;
}
break;
}
return result();
}
public alert_user_error(param: [unknown, string]): void {
toast({
message: this.format_user_error(param),
type: "is-danger",
});
}
public api_url(): string;
public api_url(endpoint: string): string;
public api_url(endpoint?: string): string {

View file

@ -2,7 +2,8 @@ import { Credentials, DoorSaved, SiteConfigModel } from "@/lib/api";
import { Door } from "@/lib/door";
import { Advent22 } from "@/plugins/advent22";
import { RemovableRef, useLocalStorage } from "@vueuse/core";
import { AxiosBasicCredentials } from "axios";
import { AxiosBasicCredentials, AxiosError } from "axios";
import { toast } from "bulma-toast";
import { acceptHMRUpdate, defineStore } from "pinia";
declare global {
@ -65,7 +66,63 @@ export const advent22Store = defineStore({
this.is_initialized = true;
for (const callback of this.on_initialized) callback();
})
.catch(this.advent22.alert_user_error);
.catch(this.alert_user_error);
},
format_user_error([reason, endpoint]: [unknown, string]): string {
let msg =
"Unbekannter Fehler, bitte wiederholen! Besteht das Problem länger, bitte Admin benachrichtigen!";
let code = "U";
const result = () => `${msg} (Fehlercode: ${code}/${endpoint})`;
if (!(reason instanceof AxiosError)) return result();
switch (reason.code) {
case "ECONNABORTED":
// API unerreichbar
msg =
"API antwortet nicht, bitte später wiederholen! Besteht das Problem länger, bitte Admin benachrichtigen!";
code = "D";
break;
case "ERR_NETWORK":
// Netzwerk nicht verbunden
msg = "Sieht aus, als sei deine Netzwerkverbindung gestört.";
code = "N";
break;
default:
if (reason.response === undefined) return result();
switch (reason.response.status) {
case 401:
// UNAUTHORIZED
msg = "Netter Versuch :)";
code = "A";
break;
case 422:
// UNPROCESSABLE ENTITY
msg = "Funktion ist kaputt, bitte Admin benachrichtigen!";
code = "I";
break;
default:
// HTTP
code = `H${reason.response.status}`;
break;
}
break;
}
return result();
},
alert_user_error(param: [unknown, string]): void {
toast({
message: this.format_user_error(param),
type: "is-danger",
});
},
update(): Promise<void> {