boxerror: assign extra fields in all cases
This commit is contained in:
@@ -8,10 +8,10 @@ const assert = require('assert'),
|
||||
|
||||
exports = module.exports = BoxError;
|
||||
|
||||
function BoxError(reason, errorOrMessage, extra) {
|
||||
function BoxError(reason, errorOrMessage, extra = {}) {
|
||||
assert.strictEqual(typeof reason, 'string');
|
||||
assert(errorOrMessage instanceof Error || typeof errorOrMessage === 'string');
|
||||
assert(typeof extra === 'object' || typeof extra === 'undefined');
|
||||
assert(typeof extra === 'object');
|
||||
|
||||
Error.call(this);
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
@@ -20,20 +20,18 @@ function BoxError(reason, errorOrMessage, extra) {
|
||||
this.reason = reason;
|
||||
this.details = {};
|
||||
|
||||
if (typeof errorOrMessage === 'undefined') {
|
||||
this.message = reason;
|
||||
} else if (typeof errorOrMessage === 'string') {
|
||||
if (typeof errorOrMessage === 'string') {
|
||||
this.message = errorOrMessage;
|
||||
} else if (errorOrMessage instanceof AggregateError) { //
|
||||
const messages = errorOrMessage.errors.map(e => e.message);
|
||||
this.message = `${errorOrMessage.message} messages: ${messages.join(',')}`;
|
||||
this.nestedError = errorOrMessage;
|
||||
Object.assign(this, extra);
|
||||
} else { // error object
|
||||
this.message = errorOrMessage.message;
|
||||
this.nestedError = errorOrMessage;
|
||||
Object.assign(this, extra);
|
||||
}
|
||||
|
||||
Object.assign(this, extra);
|
||||
}
|
||||
util.inherits(BoxError, Error);
|
||||
BoxError.ACCESS_DENIED = 'Access Denied';
|
||||
|
||||
Reference in New Issue
Block a user