UNPKG

@napp/exception

Version:

common exception. node application common library. serialization error

156 lines (155 loc) 6.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Exception = void 0; function safeStringify(obj) { try { return JSON.parse(JSON.stringify(obj)); } catch (_a) { return null; } } class Exception extends Error { constructor(message, opt) { super(message); if (opt && opt.type) { this.setErrorType(opt.type); } if (opt && opt.code) { this.setErrorCode(opt.code); } if (opt && opt.cause) { this.setCause(opt.cause); } if (opt && opt.source) { this.setSource(opt.source); } if (opt && opt.data) { this.setData(opt.data); } if (opt && (opt.isOperational === true || opt.isOperational === false)) { this.setOperational(opt.isOperational); } if (opt && opt.httpStatus) { this.setHttpStatus(opt.httpStatus); } if (typeof Error.captureStackTrace === 'function') { Error.captureStackTrace(this, Exception); } Object.setPrototypeOf(this, Exception.prototype); } setErrorType(type) { this.type = type; return this; } setErrorCode(code) { this.code = code; return this; } setCause(err) { this.cause = Exception.from(err); return this; } setSource(source) { this.source = source; return this; } setData(data) { this.data = data; return this; } setOperational(isOperational) { this.isOperational = isOperational; return this; } setHttpStatus(status) { this.httpStatus = status; return this; } toJSON() { let obj = { message: this.message, type: this.type, code: this.code, isOperational: this.isOperational, }; if (this.data) { try { obj.data = safeStringify(this.data); } catch (error) { obj.data = null; // If serialization fails, set data to null } } if (this.cause) { obj.cause = this.cause.toJSON(); } // if (typeof process !== 'undefined' && process.env?.NODE_ENV !== 'production') { // (obj as any).httpStatus = this.httpStatus; // (obj as any).source = this.source; // } return obj; } static merge(target, src) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r; if (!src || typeof src !== 'object') return target; // Error type/category target.type = (_d = (_c = (_b = (_a = src.type) !== null && _a !== void 0 ? _a : src.category) !== null && _b !== void 0 ? _b : src.errorType) !== null && _c !== void 0 ? _c : src.name) !== null && _d !== void 0 ? _d : target.type; // Error code variations target.code = (_j = (_h = (_g = (_f = (_e = src.code) !== null && _e !== void 0 ? _e : src.ecode) !== null && _f !== void 0 ? _f : src.errorCode) !== null && _g !== void 0 ? _g : src.statusCode) !== null && _h !== void 0 ? _h : src.status_code) !== null && _j !== void 0 ? _j : target.code; if (src === null || src === void 0 ? void 0 : src.cause) target.cause = Exception.from(src.cause); if ((src === null || src === void 0 ? void 0 : src.data) !== undefined) target.data = src.data; if (typeof (src === null || src === void 0 ? void 0 : src.stack) === 'string') target.stack = src.stack; // isOperational flag target.isOperational = (_m = (_l = (_k = src.isOperational) !== null && _k !== void 0 ? _k : src.operational) !== null && _l !== void 0 ? _l : (typeof src.name === 'string' && src.name.toLowerCase().includes("validation"))) !== null && _m !== void 0 ? _m : target.isOperational; // HTTP status target.httpStatus = (_r = (_q = (_p = (_o = src.httpStatus) !== null && _o !== void 0 ? _o : src.status) !== null && _p !== void 0 ? _p : src.status_code) !== null && _q !== void 0 ? _q : (typeof src.code === 'number' ? src.code : undefined)) !== null && _r !== void 0 ? _r : target.httpStatus; return target; } static httpStatusByType(type) { switch (type) { case 'Validation': return 400; case 'Authentication': return 401; case 'Authorization': return 403; case 'NotFound': return 404; case 'Conflict': return 409; case 'Timeout': return 408; case 'TooManyRequests': return 429; case 'ServiceUnavailable': return 503; case 'Internal': return 500; case 'Unsupported': return 415; case 'Database': return 500; // Database errors are often internal case 'Network': return 502; // Network errors can be considered as bad gateway case 'Unknown': return 500; // Unknown errors are often internal default: return 0; // 0 means no specific HTTP status } } static from(err, customParser) { if (customParser && typeof customParser.parse === 'function') { const parsed = customParser.parse(err); if (parsed instanceof Exception) return parsed; } if (err instanceof Exception) { return err; } if (err instanceof Error) { const exception = new Exception(err.message, { source: err }); return Exception.merge(exception, err); } if (typeof err === 'object' && typeof (err === null || err === void 0 ? void 0 : err.message) === 'string') { const exception = new Exception(err.message, { source: err }); return Exception.merge(exception, err); } return new Exception("Unknown error", { type: "Unknown", source: err }); } } exports.Exception = Exception;