From debd841cf15fa017b2f1a543f8851d6ef6e05e2c Mon Sep 17 00:00:00 2001 From: "viviane.johns" Date: Mon, 16 Dec 2024 10:14:49 +0100 Subject: [PATCH] #664 replace apiError with wlsError --- .../src/api/{ApiError.ts => WLSError.ts} | 27 ++++++++++++-- .../src/api/WLSException.ts | 18 ---------- .../src/api/fetch-utils.ts | 36 ++++++++++++------- 3 files changed, 48 insertions(+), 33 deletions(-) rename wls-gui-wahllokalsystem/src/api/{ApiError.ts => WLSError.ts} (50%) delete mode 100644 wls-gui-wahllokalsystem/src/api/WLSException.ts diff --git a/wls-gui-wahllokalsystem/src/api/ApiError.ts b/wls-gui-wahllokalsystem/src/api/WLSError.ts similarity index 50% rename from wls-gui-wahllokalsystem/src/api/ApiError.ts rename to wls-gui-wahllokalsystem/src/api/WLSError.ts index 4aca8c013..c4c07b3f9 100644 --- a/wls-gui-wahllokalsystem/src/api/ApiError.ts +++ b/wls-gui-wahllokalsystem/src/api/WLSError.ts @@ -1,22 +1,45 @@ import { STATUS_INDICATORS } from "@/constants"; -export class ApiError extends Error { +// todo: umbenennen in WLSError? +export default class WLSError extends Error { level: string; + category: string; + code: string; + service: string; + constructor({ level = STATUS_INDICATORS.ERROR, message = "Ein unbekannter Fehler ist aufgetreten, bitte den Administrator informieren.", + category = "undefined", + code = "undefined", + service = "undefined", }: { level?: string; message?: string; + category?: string; + code?: string; + service?: string; }) { // Passes the remaining parameters (including vendor-specific parameters) to the error constructor super(message); - // Retains the correct stack trace for the point at which the error was triggered this.stack = new Error().stack; // User-defined information this.level = level; this.message = message; + this.category = category; + this.code = code; + this.service = service; + } + + static isWLSException(obj: any): obj is WLSError { + return ( + obj && + typeof obj.category === "string" && + typeof obj.code === "string" && + typeof obj.message === "string" && + typeof obj.service === "string" + ); } } diff --git a/wls-gui-wahllokalsystem/src/api/WLSException.ts b/wls-gui-wahllokalsystem/src/api/WLSException.ts deleted file mode 100644 index c2a21d9d7..000000000 --- a/wls-gui-wahllokalsystem/src/api/WLSException.ts +++ /dev/null @@ -1,18 +0,0 @@ -export default class WLSException { - constructor( - public readonly category: string, - public readonly code: string, - public readonly message: string, - public readonly service: string - ) {} - - static isWLSException(obj: any): obj is WLSException { - return ( - obj && - typeof obj.category === "string" && - typeof obj.code === "string" && - typeof obj.message === "string" && - typeof obj.service === "string" - ); - } -} diff --git a/wls-gui-wahllokalsystem/src/api/fetch-utils.ts b/wls-gui-wahllokalsystem/src/api/fetch-utils.ts index ee2de90b0..a722d8191 100644 --- a/wls-gui-wahllokalsystem/src/api/fetch-utils.ts +++ b/wls-gui-wahllokalsystem/src/api/fetch-utils.ts @@ -1,5 +1,4 @@ -import { ApiError } from "@/api/ApiError"; -import WLSException from "@/api/WLSException"; +import WLSError from "@/api/WLSError"; import { STATUS_INDICATORS } from "@/constants"; /** @@ -84,11 +83,11 @@ export function patchConfig(body: any): RequestInit { */ export function defaultResponseHandler( response: Response, - errorMessage = "Es ist ein unbekannter Fehler aufgetreten." + errorMessage = "Es ist ein unbekannter Fehler aufgetreten." // todo: warum hier nochmal die message wenn im error schon ein default wert steht ): void { if (!response.ok) { if (response.status === 403) { - throw new ApiError({ + throw new WLSError({ level: STATUS_INDICATORS.ERROR, message: "Sie haben nicht die nötigen Rechte um diese Aktion durchzuführen.", @@ -96,7 +95,7 @@ export function defaultResponseHandler( } else if (response.type === "opaqueredirect") { location.reload(); } - throw new ApiError({ + throw new WLSError({ level: STATUS_INDICATORS.WARNING, message: errorMessage, }); @@ -105,15 +104,15 @@ export function defaultResponseHandler( /** * Default catch handler for all service requests. - * Currently only throws an ApiError + * Currently only throws an ApiError // todo: api error ersetzen * @param error The error object from fetch command - * @param errorMessage The error message to be included in the ApiError object. + * @param errorMessage The error message to be included in the ApiError object. // todo: api error ersetzen */ export function defaultCatchHandler( error: Error, errorMessage = "Es ist ein unbekannter Fehler aufgetreten." ): PromiseLike { - throw new ApiError({ + throw new WLSError({ level: STATUS_INDICATORS.WARNING, message: errorMessage, }); @@ -129,21 +128,32 @@ export function wlsResponseHandler(response: Response): Promise { export function wlsCatchHandler(response: Response): PromiseLike { if (response.status === 204) { - throw new ApiError({ + throw new WLSError({ level: STATUS_INDICATORS.INFO, message: "Es konnten keine Daten gefunden werden", }); } if (response.status === 400) { return response.json().then((content) => { - if (WLSException.isWLSException(content)) { - return Promise.reject(new ApiError({ message: content.message })); + if (WLSError.isWLSException(content)) { + // todo: wird das noch benötigt, wenn es nur noch die wls exception gibt + return Promise.reject( + new WLSError({ + level: STATUS_INDICATORS.ERROR, + message: content.message, + category: content.category, + code: content.code, + service: content.service, + }) + ); } else { - return Promise.reject(new ApiError({ message: "Error: Bad Request" })); + return Promise.reject(new WLSError({ message: "Error: Bad Request" })); } }); } else { - return Promise.reject(new ApiError({ message: "unbekannter fehler" })); + return Promise.reject( + new WLSError({ message: "Ein unbekannter Fehler ist aufgetreten" }) + ); } }