32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
export class PlatformError extends Error {
|
|
constructor(message, { code = 'PLATFORM_ERROR', status = 500, details = null, expose = false, cause = undefined } = {}) {
|
|
super(String(message || 'Unbekannter Plattformfehler'), { cause });
|
|
this.name = 'PlatformError';
|
|
this.code = String(code || 'PLATFORM_ERROR');
|
|
this.status = Number.isInteger(status) ? status : 500;
|
|
this.details = details;
|
|
this.expose = Boolean(expose || this.status < 500);
|
|
}
|
|
}
|
|
|
|
export function normalizePlatformError(error) {
|
|
if (error instanceof PlatformError) return error;
|
|
return new PlatformError(error?.message || 'Interner Serverfehler', {
|
|
code: error?.code || 'INTERNAL_ERROR',
|
|
status: Number.isInteger(error?.status) ? error.status : 500,
|
|
details: error?.details || null,
|
|
expose: Boolean(error?.expose),
|
|
cause: error,
|
|
});
|
|
}
|
|
|
|
export function platformErrorPayload(error, requestId = null) {
|
|
const normalized = normalizePlatformError(error);
|
|
return {
|
|
error: normalized.expose ? normalized.message : 'Interner Serverfehler',
|
|
code: normalized.code,
|
|
request_id: requestId || null,
|
|
...(normalized.expose && normalized.details ? { details: normalized.details } : {}),
|
|
};
|
|
}
|