Skip to content

Commit

Permalink
#664 replace apiError with wlsError
Browse files Browse the repository at this point in the history
  • Loading branch information
vjohnslhm committed Dec 16, 2024
1 parent f1e3b78 commit debd841
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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"
);
}
}
18 changes: 0 additions & 18 deletions wls-gui-wahllokalsystem/src/api/WLSException.ts

This file was deleted.

36 changes: 23 additions & 13 deletions wls-gui-wahllokalsystem/src/api/fetch-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ApiError } from "@/api/ApiError";
import WLSException from "@/api/WLSException";
import WLSError from "@/api/WLSError";
import { STATUS_INDICATORS } from "@/constants";

/**
Expand Down Expand Up @@ -84,19 +83,19 @@ 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.",
});
} else if (response.type === "opaqueredirect") {
location.reload();
}
throw new ApiError({
throw new WLSError({
level: STATUS_INDICATORS.WARNING,
message: errorMessage,
});
Expand All @@ -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<never> {
throw new ApiError({
throw new WLSError({
level: STATUS_INDICATORS.WARNING,
message: errorMessage,
});
Expand All @@ -129,21 +128,32 @@ export function wlsResponseHandler(response: Response): Promise<Response> {

export function wlsCatchHandler(response: Response): PromiseLike<never> {
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" })
);
}
}

Expand Down

0 comments on commit debd841

Please sign in to comment.