@eleva-io/erp-sdk
Version:
SDK oficial para el ERP de Eleva
53 lines • 1.63 kB
JavaScript
import { z } from 'zod';
import { CATALOG } from './catalog';
const responseSchema = z.object({
code: z.string(),
kind: z.enum(['domain', 'system']),
status: z.number(),
message: z.string(),
details: z.unknown().optional(),
});
export class AppError extends Error {
code;
kind;
status;
details;
constructor(code, details, message) {
const entry = CATALOG[code];
const defaultMessage = typeof entry.message === 'function'
? entry.message(details)
: entry.message;
super(message ?? defaultMessage);
this.name = 'AppError';
this.code = code;
this.kind = entry.kind;
this.status = entry.status;
this.details = entry.details.parse(details);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
is(code) {
return this.code === code;
}
toJSON() {
return {
code: this.code,
kind: this.kind,
status: this.status,
message: this.message,
details: this.details,
};
}
static fromResponse(body) {
const parsed = responseSchema.parse(body);
if (!(parsed.code in CATALOG)) {
throw new Error(`Unknown error code: ${parsed.code}`);
}
const code = parsed.code;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const details = CATALOG[code].details.parse(parsed.details ?? {});
return new AppError(code, details, parsed.message);
}
}
//# sourceMappingURL=app_error.js.map