Skip to content

Commit

Permalink
Merge pull request #3645 from tapuzzo-fsi/master
Browse files Browse the repository at this point in the history
[patch] Fix incorrect handling of Error objects in handleError() and add test…
  • Loading branch information
mikermcneil committed Mar 9, 2016
2 parents 93fec00 + cda9636 commit 8b506a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/hooks/responses/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,11 @@ function _mixin_jsonx(req, res) {
if (!jsonSerializedErr.stack || !jsonSerializedErr.message) {
jsonSerializedErr.message = err.message;
jsonSerializedErr.stack = err.stack;

return jsonSerializedErr;
}
return jsonSerializedErr;
}
catch (e){
return {message: err.message, stack: err.stack};
return {name: err.name, message: err.message, stack: err.stack};
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/unit/req.errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ describe('request that causes an error', function (){
'responses'
]
});
var saveServerError;

// Restore the default error handler after tests that change it
afterEach(function () {
if (saveServerError) {
sails.registry.responses.serverError = saveServerError;
saveServerError = undefined;
}
});

it('should return the expected error when something throws', function (done) {

Expand All @@ -40,6 +49,7 @@ describe('request that causes an error', function (){
var ERROR = 'oh no I forgot my keys';
var CHECKPOINT = 'made it';

saveServerError = sails.registry.responses.serverError;
sails.registry.responses.serverError = function (err) {
assert.deepEqual(ERROR, err);
this.res.send(500, CHECKPOINT);
Expand All @@ -56,4 +66,22 @@ describe('request that causes an error', function (){

});

it('should return the expected error when something throws an Error object', function (done) {

var MESSAGE = 'oh no I forgot my keys again';
var ERROR = new Error(MESSAGE);

sails.get('/errors/3', function (req, res) {
throw ERROR;
});

sails.request('GET /errors/3', {}, function (err) {
assert.deepEqual(err.status, 500);
assert.deepEqual(typeof(err.body), 'object');
assert.deepEqual(err.body.message, MESSAGE);
done();
});

});

});

0 comments on commit 8b506a2

Please sign in to comment.