Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[patch] Fix incorrect handling of Error objects in handleError() and add test… #3645

Merged
merged 1 commit into from Mar 9, 2016
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
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();
});

});

});