UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

68 lines (67 loc) 2.88 kB
'use strict'; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("util"); const runtime_store_1 = __importDefault(require("./runtime-store")); const default_1 = __importDefault(require("./util_modules/constants/lib/default")); class CatalystError extends Error { constructor(message, options = {}) { var _a, _b; super(message); this.name = default_1.default.catalystError; this.status = options.status || 500; this.exit = options.exit || 1; this.original = options.original; this.context = options.context; this.errorId = options.errorId; this.arg = options.arg; this.skipHelp = options.skipHelp || false; try { if (!this.skipHelp) { const stackArr = ((_a = this.stack) === null || _a === void 0 ? void 0 : _a.split(/\n {4}at /)) || []; this.fileName = options.fileName === undefined && stackArr[1] !== undefined ? stackArr[1].replace(/\.[^.]+$/, '').replace(/^[^(]+\(/, '') : (_b = options.fileName) === null || _b === void 0 ? void 0 : _b.replace(/\.[^.]+$/, ''); runtime_store_1.default.set(`[log]["${this.fileName}"]`, runtime_store_1.default.get(`[log]["${this.fileName}"]`, []).concat(this)); } } catch (e) { runtime_store_1.default.set('log.faultyError', runtime_store_1.default.get('log.faultyError', []).concat(this)); } } static getErrorInstance(unknownError, { message, skipHelp, fileName } = {}) { if (unknownError instanceof CatalystError) { if (message) { unknownError.message = message; } return unknownError; } if (unknownError instanceof Error) { return new CatalystError(message || unknownError.message, { original: unknownError, skipHelp: skipHelp === undefined ? true : skipHelp, fileName }); } const newError = new CatalystError(message || 'Malformed error', { original: (0, util_1.inspect)(unknownError), skipHelp: skipHelp === undefined ? true : skipHelp }); if (typeof unknownError === 'object') { if ('message' in unknownError) { newError.message = unknownError.message; } if ('stack' in unknownError) { newError.stack = unknownError.stack; } if ('name' in unknownError) { newError.name = unknownError.name; } } return newError; } } exports.default = CatalystError;