UNPKG

@swoft/platform-core-errors

Version:

Standardized error handling for Swoft applications with UX/DX suggestions

95 lines (91 loc) 2.29 kB
// src/BaseDomainError.ts var BaseDomainError = class extends Error { layer = "domain"; suggestions; cause; constructor(message, suggestions, cause) { super(message); this.name = new.target.name; this.suggestions = suggestions; this.cause = cause; Object.setPrototypeOf(this, new.target.prototype); } }; // src/BaseApplicationError.ts var BaseApplicationError = class extends Error { layer = "application"; suggestions; cause; constructor(message, suggestions, cause) { super(message); this.name = new.target.name; this.suggestions = suggestions; this.cause = cause; Object.setPrototypeOf(this, new.target.prototype); } }; // src/BaseInfrastructureError.ts var BaseInfrastructureError = class extends Error { layer = "infrastructure"; suggestions; cause; constructor(message, suggestions, cause) { super(message); this.name = new.target.name; this.suggestions = suggestions; this.cause = cause; Object.setPrototypeOf(this, new.target.prototype); } }; // src/utils/formatErrorForLog.ts function formatErrorForLog(error) { if (error instanceof Error && !("code" in error)) { return { name: error.constructor.name, message: error.message, stack: error.stack, timestamp: (/* @__PURE__ */ new Date()).toISOString() }; } return { name: error.constructor.name, code: error.code || "UNKNOWN_ERROR", layer: error.layer || "unknown", message: error.message, suggestions: error.suggestions ? { dx: error.suggestions.dx } : void 0, cause: formatCause(error.cause), timestamp: (/* @__PURE__ */ new Date()).toISOString() }; } function formatCause(cause) { if (cause instanceof Error) return cause.stack || cause.message; if (typeof cause === "string") return cause; try { return JSON.stringify(cause); } catch { return "Unserializable cause"; } } // src/utils/logger.ts import pino from "pino"; var logger = pino({ name: "swoft", level: "info", transport: { target: "pino-pretty", options: { colorize: true, ignore: "pid,hostname" } } }); export { BaseApplicationError, BaseDomainError, BaseInfrastructureError, formatErrorForLog, logger }; //# sourceMappingURL=index.mjs.map