36 lines
790 B
TypeScript
36 lines
790 B
TypeScript
export class ApiError extends Error {
|
|
constructor(
|
|
message: string,
|
|
public readonly status?: number,
|
|
public readonly code?: string,
|
|
public readonly meta?: Record<string, unknown>,
|
|
) {
|
|
super(message);
|
|
this.name = 'ApiError';
|
|
}
|
|
}
|
|
|
|
export function isApiError(value: unknown): value is ApiError {
|
|
return value instanceof ApiError;
|
|
}
|
|
|
|
export function getApiErrorMessage(error: unknown, fallback: string): string {
|
|
if (isApiError(error)) {
|
|
if (error.message) {
|
|
return error.message;
|
|
}
|
|
|
|
if (error.status && error.status >= 500) {
|
|
return 'Der Server hat nicht reagiert. Bitte versuche es später erneut.';
|
|
}
|
|
|
|
return fallback;
|
|
}
|
|
|
|
if (error instanceof Error && error.message) {
|
|
return error.message;
|
|
}
|
|
|
|
return fallback;
|
|
}
|