UNPKG

@sentzunhat/zacatl

Version:

A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.

80 lines 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomError = void 0; const uuid_1 = require("../third-party/uuid.js"); class CustomError extends Error { custom; code; reason; metadata; error; time; id; correlationId; component; operation; constructor({ message, code, reason, metadata, error, correlationId, component, operation, }) { super(message); this.id = (0, uuid_1.uuidv4)(); this.correlationId = correlationId ?? (0, uuid_1.uuidv4)(); this.custom = true; this.name = this.constructor.name; this.code = code; this.reason = reason; this.metadata = metadata; this.error = error; this.time = new Date(); this.component = component; this.operation = operation; if (typeof Error.captureStackTrace === 'function') { Error.captureStackTrace(this, this.constructor); } } toJSON() { const out = { name: this.name, message: this.message, time: this.time.toISOString(), id: this.id, correlationId: this.correlationId, custom: this.custom, }; if (this.code != null) out.code = this.code; if (this.reason != null) out.reason = this.reason; if (this.metadata != null) out.metadata = this.metadata; if (this.component != null) out.component = this.component; if (this.operation != null) out.operation = this.operation; if (this.stack != null) out.stack = this.stack; if (this.error != null) { const e = { name: this.error.name, message: this.error.message, }; if (this.error.stack != null) e.stack = this.error.stack; out.error = e; } return out; } toString() { return [ `[${this.time.toISOString()}] ${this.name}: ${this.message}`, `\nCorrelationId: ${this.correlationId}`, this.code != null ? `\nCode: ${this.code}` : '', this.component != null ? `\nComponent: ${this.component}` : '', this.operation != null ? `\nOperation: ${this.operation}` : '', this.reason != null ? `\nReason: ${this.reason}` : '', this.metadata != null ? `\nMetadata: ${JSON.stringify(this.metadata, null, 2)}` : '', this.error != null ? `\nCaused by: ${this.error.toString()}` : '', this.stack != null ? `\nStack: ${this.stack}` : '', ].join(''); } } exports.CustomError = CustomError; //# sourceMappingURL=custom.js.map