@dapi-co/derror
Version:
Dapi error class for Moleculer framework.
127 lines (126 loc) • 4.5 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var MoleculerError = require('moleculer').Errors.MoleculerError;
var errMap = {};
function SetErrorMap(newErrMap) {
errMap = newErrMap;
}
exports.SetErrorMap = SetErrorMap;
//PERF: Can optimize stack by knowing call depth
var DError = /** @class */ (function () {
function DError(prevError, msg, code, type, data, external) {
if (code === void 0) { code = 500; }
if (type === void 0) { type = ''; }
if (data === void 0) { data = {}; }
if (external === void 0) { external = false; }
this.msg = msg;
this.code = code;
this.type = type;
this.data = data;
this.external = external;
this.stack = '';
this.message = '';
this.name = 'DError';
//In case someone sends data as null
if (!data)
data = {};
//If second parameter is type and third is data, use the error lookup table
if (typeof (code) === "object") {
type = msg;
data = code;
if (type in errMap) {
msg = errMap[type].msg;
code = errMap[type].code;
external = errMap[type].external;
this.msg = msg;
this.code = code;
this.type = type;
this.data = data;
this.external = external;
}
else {
code = 500;
}
}
this.stack = '\n' + (this.stack || Error().stack.split('\n', 3)[2]);
this.msg = msg;
this.external = external;
if (!prevError) {
this.data.errStack = [{
msg: this.msg,
code: this.code,
type: this.type,
external: this.external,
data: __assign({}, this.data) //To avoid circular references
}];
return;
}
//Cross network errors will be DError but will not pass instanceof, so check name
if (!(prevError instanceof DError) && prevError.name !== 'DError') {
this.data.errStack = [{
msg: this.msg,
code: this.code,
type: this.type,
external: this.external,
data: __assign({}, this.data)
}, {}];
if (prevError instanceof MoleculerError)
this.data.errStack[1] = {
msg: prevError.message,
code: prevError.message,
type: prevError.type,
data: prevError.data
};
else if (prevError instanceof Error)
this.data.errStack[1] = {
msg: prevError.message,
code: prevError.code,
type: prevError.type,
data: prevError.data
};
else if (typeof prevError === 'string') {
this.data.errStack[1] = {
msg: prevError,
code: 500,
type: '',
external: false,
data: {}
};
}
if (prevError.stack)
this.stack += '\n' + prevError.stack;
return;
}
this.stack += prevError.stack;
prevError.data.errStack.unshift({
msg: this.msg,
code: this.code,
type: this.type,
external: this.external,
data: __assign({}, this.data)
});
this.data.errStack = prevError.data.errStack;
//Older external errors take precedence to be shown to the user
if (prevError.external) {
this.message = prevError.message;
this.msg = prevError.msg;
this.code = prevError.code;
this.type = prevError.type;
this.data = prevError.data;
this.external = true;
}
}
return DError;
}());
exports.DError = DError;