zcatalyst-cli
Version:
Command Line Tool for CATALYST
72 lines (71 loc) • 3.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const util_1 = require("util");
const utils_js_1 = require("./utils.js");
const DEFAULT_DOC = (0, path_1.join)(__dirname, '../docs');
class CatalystError extends Error {
constructor(message, options = {}) {
var _a, _b;
super(message);
this.name = '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 /)) || [];
const errorPath = options.filePath === undefined && stackArr[1] !== undefined
? stackArr[1].replace(/\.[^.]+$/, '').replace(/^[^(]+\(/, '')
: (_b = options.filePath) === null || _b === void 0 ? void 0 : _b.replace(/\.[^.]+$/, '');
if (!errorPath) {
throw new CatalystError('Invalid error path', { skipHelp: true });
}
options.docPath = options.docPath || DEFAULT_DOC;
this.filePath = errorPath.replace((0, path_1.dirname)(options.docPath), options.docPath);
utils_js_1.runtime.set(`context.log["${this.filePath}"]`, utils_js_1.runtime
.get(`[context][log]["${this.filePath}"]`, [])
.concat(this));
}
}
catch (e) {
utils_js_1.runtime.set('context.log.faultyError', utils_js_1.runtime.get('context.log.faultyError', []).concat(this));
}
}
static getErrorInstance(unknownError, { message, skipHelp, docPath } = {}) {
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,
docPath
});
}
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;