UNPKG

@neylion/exceptions

Version:

Exceptions commonly used within ney projects

49 lines (48 loc) 2.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const stack_utils_1 = __importDefault(require("stack-utils")); const stackTrace = new stack_utils_1.default({ cwd: process.cwd(), internals: stack_utils_1.default.nodeInternals() }); class Exception extends Error { constructor(message, innerException, stack) { super(); this.name = "Exception"; this.statusCode = 500; this.type = "unknown"; this.typeDescription = "Generic exception without specified type."; this.isApplicationError = true; this.stack = stack || this.stack; this.message = message; this.location = this.stack ? getCallerHistory(this.stack) : ["Could not find error stack trace!"]; this.innerException = cloneInnerException(innerException); } } exports.Exception = Exception; function getCallerHistory(stack, limit = 5, callsToIgnore = 0) { const errorStack = stackTrace.clean(stack); const callSites = errorStack.split("\n"); const errorTrace = []; for (let i = callsToIgnore; i < limit + callsToIgnore; i++) { errorTrace.push(callSites[i]); } errorTrace.push("For a more detailed stack trace check error stack trace"); return errorTrace; } function cloneInnerException(innerException) { let clonedInnerException; if (innerException) { clonedInnerException = {}; if (innerException.isApplicationError) { Object.assign(clonedInnerException, innerException); } else { // Manual shallow copy of object (as error properties typically are not enumerable and spread operators does not work) Object.getOwnPropertyNames(innerException).forEach(key => { clonedInnerException[key] = innerException[key]; }); } } return clonedInnerException; }