Skip to content

Commit

Permalink
fix: properly handle non-errors thrown in domains
Browse files Browse the repository at this point in the history
  • Loading branch information
DonutEspresso committed Mar 13, 2019
1 parent 6259b24 commit b953997
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 9 additions & 3 deletions lib/server.js
Expand Up @@ -1309,13 +1309,19 @@ Server.prototype._routeErrorResponse = function _routeErrorResponse(
return;
}

// Expose only knonw errors
if (_.isNumber(err.statusCode)) {
// only automatically send errors that are known (e.g., restify-errors)
if (err && _.isNumber(err.statusCode)) {
res.send(err);
return;
}

res.send(new errors.InternalError(emitErr || err));
var finalErr = emitErr || err;
if (!(finalErr instanceof Error)) {
// if user land domain threw a string or number or any other
// non-error object, handle it here as best we can.
finalErr = (finalErr && finalErr.toString()) || '';
}
res.send(new errors.InternalError(finalErr));
});
};

Expand Down
6 changes: 4 additions & 2 deletions test/server.test.js
Expand Up @@ -2505,9 +2505,10 @@ test('uncaughtException should handle thrown undefined literal', function(t) {
}
);

CLIENT.get('/foo', function(err, _, res) {
CLIENT.get('/foo', function(err, _, res, data) {
t.ok(err);
t.equal(res.statusCode, 500);
t.equal(data.message, '');
t.end();
});
});
Expand Down Expand Up @@ -2535,8 +2536,9 @@ test('uncaughtException should handle thrown Number', function(t) {
}
);

CLIENT.get('/foo', function(err, _, res) {
CLIENT.get('/foo', function(err, _, res, data) {
t.ok(err);
t.equal(data.message, '1');
t.equal(res.statusCode, 500);
t.end();
});
Expand Down

0 comments on commit b953997

Please sign in to comment.