Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/baseClasses/HttpError.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,22 @@ HttpError.prototype.code = 'Error';
*/
HttpError.prototype.toJSON = function toJSON() {
var self = this;
return JSON.stringify(self.body);

var message = '';

// if we have a cause, get the full VError toString() without the current
// error name. verbose check, self.cause can exist but returns undefined
if (self.cause && typeof self.cause === 'function' && self.cause()) {
var fullString = self.toString();
message = fullString.substr(fullString.indexOf(' ') + 1);
} else {
message = self.body.message;
}

return {
code: self.body.code,
message: message
};
};


Expand Down
25 changes: 14 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('restify-errors node module.', function() {
assert.equal(myErr instanceof Error, true);

assert.equal(myErr.code, 'Error');
assert.equal(myErr.toJSON(), JSON.stringify({
assert.deepEqual(JSON.stringify(myErr), JSON.stringify({
code: 'Error',
message: ''
}));
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('restify-errors node module.', function() {
assert.equal(myErr.message, '');

// assert stringification
assert.equal(myErr.toJSON(), JSON.stringify({
assert.equal(JSON.stringify(myErr), JSON.stringify({
code: 'BadGateway',
message: ''
}));
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('restify-errors node module.', function() {
assert.equal(myErr.restCode, 'Error');

// assert stringification
assert.equal(myErr.toJSON(), JSON.stringify({
assert.equal(JSON.stringify(myErr), JSON.stringify({
code: 'Error',
message: ''
}));
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('restify-errors node module.', function() {
assert.equal(myErr.restCode, 'BadDigest');

// assert stringification
assert.equal(myErr.toJSON(), JSON.stringify({
assert.equal(JSON.stringify(myErr), JSON.stringify({
code: 'BadDigest',
message: ''
}));
Expand Down Expand Up @@ -552,9 +552,10 @@ describe('restify-errors node module.', function() {
assert.equal(err.we_cause, underlyingErr);

// assert stringification
assert.equal(err.toJSON(), JSON.stringify({
assert.equal(JSON.stringify(err), JSON.stringify({
code: 'Execution',
message: 'bad joystick input'
message: 'bad joystick input; ' +
'caused by Error: underlying error!'
}));
});

Expand Down Expand Up @@ -588,9 +589,10 @@ describe('restify-errors node module.', function() {
assert.deepEqual(err.context.baz, [1,2,3]);

// assert stringification
assert.equal(err.toJSON(), JSON.stringify({
assert.equal(JSON.stringify(err), JSON.stringify({
code: 'Execution',
message: 'bad joystick input'
message: 'bad joystick input; ' +
'caused by Error: underlying error!'
}));
});

Expand All @@ -606,9 +608,10 @@ describe('restify-errors node module.', function() {
assert.equal(err instanceof restifyErrors.Executionerror, true);

// assert stringification
assert.equal(err.toJSON(), JSON.stringify({
assert.equal(JSON.stringify(err), JSON.stringify({
code: 'Execution',
message: 'bad joystick input'
message: 'bad joystick input; ' +
'caused by Error: underlying error!'
}));
});

Expand Down Expand Up @@ -636,7 +639,7 @@ describe('restify-errors node module.', function() {
assert.equal(err.body.message, 'the horror');

// assert stringification
assert.equal(err.toJSON(), JSON.stringify({
assert.equal(JSON.stringify(err), JSON.stringify({
code: 'NotAcceptable',
message: 'the horror'
}));
Expand Down