Files
fotospiel-app/resources/js/admin/lib/apiError.ts

67 lines
1.6 KiB
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;
}
export type ApiErrorEventDetail = {
message: string;
status?: number;
code?: string;
meta?: Record<string, unknown>;
};
export const API_ERROR_EVENT = 'admin:api:error';
export function emitApiErrorEvent(detail: ApiErrorEventDetail): void {
if (typeof window === 'undefined') {
return;
}
window.dispatchEvent(new CustomEvent<ApiErrorEventDetail>(API_ERROR_EVENT, { detail }));
}
export function registerApiErrorListener(handler: (detail: ApiErrorEventDetail) => void): () => void {
if (typeof window === 'undefined') {
return () => {};
}
const listener = (event: Event) => {
const customEvent = event as CustomEvent<ApiErrorEventDetail>;
handler(customEvent.detail);
};
window.addEventListener(API_ERROR_EVENT, listener as EventListener);
return () => window.removeEventListener(API_ERROR_EVENT, listener as EventListener);
}