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

fix: properly handle non-errors thrown in domains #1757

Merged
merged 3 commits into from Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be followed for throw res;, don't know if we want to guard against that.

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()) || '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this behave if finalErr is false or 0? Do we expect both of those to return ''

}
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, '');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could technically send 'undefined' over the wire, but don't feel strongly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well if you do throw undefined, you have a different problem as well

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