UNPKG

@eleva-io/erp-sdk

Version:

SDK oficial para el ERP de Eleva

57 lines 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppError = void 0; const zod_1 = require("zod"); const catalog_1 = require("./catalog"); const responseSchema = zod_1.z.object({ code: zod_1.z.string(), kind: zod_1.z.enum(['domain', 'system']), status: zod_1.z.number(), message: zod_1.z.string(), details: zod_1.z.unknown().optional(), }); class AppError extends Error { code; kind; status; details; constructor(code, details, message) { const entry = catalog_1.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_1.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_1.CATALOG[code].details.parse(parsed.details ?? {}); return new AppError(code, details, parsed.message); } } exports.AppError = AppError; //# sourceMappingURL=app_error.js.map