53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
export type JsonPrimitive = string | number | boolean | null;
|
|
export type JsonValue = JsonPrimitive | JsonValue[] | { [key: string]: JsonValue };
|
|
|
|
export type CatalogErrorDefinition = {
|
|
code: string;
|
|
status: number;
|
|
i18nKey: string;
|
|
message?: string;
|
|
};
|
|
|
|
export const catalogRegistry = Object.freeze({}) satisfies Record<string, CatalogErrorDefinition>;
|
|
|
|
export class CatalogError extends Error {
|
|
readonly code: string;
|
|
readonly status: number;
|
|
readonly i18nKey: string;
|
|
readonly details?: Record<string, JsonValue>;
|
|
|
|
constructor(definition: CatalogErrorDefinition, details?: Record<string, JsonValue>) {
|
|
super(definition.message ?? definition.code);
|
|
this.name = "CatalogError";
|
|
this.code = definition.code;
|
|
this.status = definition.status;
|
|
this.i18nKey = definition.i18nKey;
|
|
this.details = details;
|
|
}
|
|
}
|
|
|
|
export const foundationErrors = {
|
|
forbidden: { code: "foundation.forbidden", status: 403, i18nKey: "foundation.errors.forbidden" },
|
|
notFound: { code: "foundation.not_found", status: 404, i18nKey: "foundation.errors.notFound" },
|
|
idempotencyConflict: {
|
|
code: "foundation.idempotency_conflict",
|
|
status: 409,
|
|
i18nKey: "foundation.errors.idempotencyConflict",
|
|
},
|
|
idempotencyInProgress: {
|
|
code: "foundation.idempotency_in_progress",
|
|
status: 409,
|
|
i18nKey: "foundation.errors.idempotencyInProgress",
|
|
},
|
|
} as const satisfies Record<string, CatalogErrorDefinition>;
|
|
|
|
export function toPinoLogObject(error: unknown): Record<string, unknown> {
|
|
if (error instanceof CatalogError) {
|
|
return { error: { name: error.name, message: error.message, code: error.code, status: error.status, i18nKey: error.i18nKey, details: error.details } };
|
|
}
|
|
if (error instanceof Error) {
|
|
return { error: { name: error.name, message: error.message, stack: error.stack } };
|
|
}
|
|
return { error };
|
|
}
|