@swoft/platform-core-errors
Version:
Standardized error handling for Swoft applications with UX/DX suggestions
1 lines • 4.8 kB
Source Map (JSON)
{"version":3,"sources":["../src/BaseDomainError.ts","../src/BaseApplicationError.ts","../src/BaseInfrastructureError.ts","../src/utils/formatErrorForLog.ts","../src/utils/logger.ts"],"sourcesContent":["import { IAppError } from './IAppError';\n\nexport abstract class BaseDomainError extends Error implements IAppError {\n abstract readonly code: string;\n readonly layer: IAppError['layer'] = 'domain';\n readonly suggestions: { ux: string[]; dx: string[] };\n readonly cause?: unknown;\n\n protected constructor(\n message: string,\n suggestions: { ux: string[]; dx: string[] },\n cause?: unknown\n ) {\n super(message);\n this.name = new.target.name;\n this.suggestions = suggestions;\n this.cause = cause;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}","import { IAppError } from './IAppError';\n\nexport abstract class BaseApplicationError extends Error implements IAppError {\n abstract readonly code: string;\n readonly layer: IAppError['layer'] = 'application';\n readonly suggestions: { ux: string[]; dx: string[] };\n readonly cause?: unknown;\n\n protected constructor(\n message: string,\n suggestions: { ux: string[]; dx: string[] },\n cause?: unknown\n ) {\n super(message);\n this.name = new.target.name;\n this.suggestions = suggestions;\n this.cause = cause;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}","import { IAppError } from './IAppError';\n\nexport abstract class BaseInfrastructureError extends Error implements IAppError {\n abstract readonly code: string;\n readonly layer: IAppError['layer'] = 'infrastructure';\n readonly suggestions: { ux: string[]; dx: string[] };\n readonly cause?: unknown;\n\n public constructor(\n message: string,\n suggestions: { ux: string[]; dx: string[] },\n cause?: unknown\n ) {\n super(message);\n this.name = new.target.name;\n this.suggestions = suggestions;\n this.cause = cause;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n","import { IAppError } from '../IAppError';\n\nexport function formatErrorForLog(error: IAppError | Error | any): Record<string, any> {\n // Handle regular JavaScript errors that don't have IAppError structure\n if (error instanceof Error && !('code' in error)) {\n return {\n name: error.constructor.name,\n message: error.message,\n stack: error.stack,\n timestamp: new Date().toISOString(),\n };\n }\n\n // Handle IAppError instances\n return {\n name: error.constructor.name,\n code: error.code || 'UNKNOWN_ERROR',\n layer: error.layer || 'unknown',\n message: error.message,\n suggestions: error.suggestions ? {\n dx: error.suggestions.dx,\n } : undefined,\n cause: formatCause(error.cause),\n timestamp: new Date().toISOString(),\n };\n}\n\nfunction formatCause(cause: unknown): string | undefined {\n if (cause instanceof Error) return cause.stack || cause.message;\n if (typeof cause === 'string') return cause;\n try {\n return JSON.stringify(cause);\n } catch {\n return 'Unserializable cause';\n }\n}","import pino from 'pino';\n\nexport const logger = pino({\n name: 'swoft',\n level: 'info',\n transport: {\n target: 'pino-pretty',\n options: {\n colorize: true,\n ignore: 'pid,hostname',\n },\n },\n});"],"mappings":";AAEO,IAAe,kBAAf,cAAuC,MAA2B;AAAA,EAE9D,QAA4B;AAAA,EAC5B;AAAA,EACA;AAAA,EAEC,YACR,SACA,aACA,OACA;AACA,UAAM,OAAO;AACb,SAAK,OAAO,WAAW;AACvB,SAAK,cAAc;AACnB,SAAK,QAAQ;AACb,WAAO,eAAe,MAAM,WAAW,SAAS;AAAA,EAClD;AACF;;;ACjBO,IAAe,uBAAf,cAA4C,MAA2B;AAAA,EAEnE,QAA4B;AAAA,EAC5B;AAAA,EACA;AAAA,EAEC,YACR,SACA,aACA,OACA;AACA,UAAM,OAAO;AACb,SAAK,OAAO,WAAW;AACvB,SAAK,cAAc;AACnB,SAAK,QAAQ;AACb,WAAO,eAAe,MAAM,WAAW,SAAS;AAAA,EAClD;AACF;;;ACjBO,IAAe,0BAAf,cAA+C,MAA2B;AAAA,EAEtE,QAA4B;AAAA,EAC5B;AAAA,EACA;AAAA,EAEF,YACL,SACA,aACA,OACA;AACA,UAAM,OAAO;AACb,SAAK,OAAO,WAAW;AACvB,SAAK,cAAc;AACnB,SAAK,QAAQ;AACb,WAAO,eAAe,MAAM,WAAW,SAAS;AAAA,EAClD;AACF;;;ACjBO,SAAS,kBAAkB,OAAqD;AAErF,MAAI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