nani
Version:
Better error handling for Node
67 lines • 2.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NaniError = void 0;
const lodash_1 = require("lodash");
class NaniError extends Error {
constructor(...args) {
super();
const options = this.constructor._normalizeArgs(args);
this.shortMessage = options.shortMessage;
this.cause = options.cause || null;
this.info = options.info || null;
if (this.shortMessage) {
this.usedDefaultMessage = false;
}
else {
const info = this.info || {};
this.shortMessage = this.constructor
.getDefaultMessage(info);
this.usedDefaultMessage = true;
}
let message;
const { prefix } = this.constructor;
if (prefix) {
message = `${prefix} : ${this.shortMessage}`;
}
else {
message = this.shortMessage;
}
if (this.cause && !options.hideCauseMessage) {
this.message = `${message} : ${this.cause.message}`;
}
else {
this.message = message;
}
}
static get fullName() {
if (!Object.prototype.hasOwnProperty.call(this, "_fullName")) {
this._fullName = this._getFullName();
}
return this._fullName;
}
static getDefaultMessage(info) {
return "An error has occurred";
}
static _getFullName() {
const sup = Object.getPrototypeOf(this);
if (!sup.fullName)
return `Error.${this.name}`;
return `${sup.fullName}.${this.name}`;
}
static _normalizeArgs(args) {
const options = {};
if ((0, lodash_1.isString)(args[0]))
options.shortMessage = args.shift();
if (args[0] instanceof Error)
options.cause = args.shift();
return (0, lodash_1.assign)(options, args[0]);
}
get name() {
return this.constructor.name;
}
get fullName() {
return this.constructor.fullName;
}
}
exports.NaniError = NaniError;
//# sourceMappingURL=nani-error.js.map