Skip to content

Commit 0f398a6

Browse files
committed
fix: properly handle non-errors thrown in domains
1 parent 6259b24 commit 0f398a6

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/server.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,13 +1309,19 @@ Server.prototype._routeErrorResponse = function _routeErrorResponse(
13091309
return;
13101310
}
13111311

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

1318-
res.send(new errors.InternalError(emitErr || err));
1318+
var finalErr = emitErr || err;
1319+
if (!(finalErr instanceof Error)) {
1320+
// if user land threw domain or string or numbers, handle it here
1321+
// as best we can.
1322+
finalErr = (finalErr && finalErr.toString()) || '';
1323+
}
1324+
res.send(new errors.InternalError(finalErr));
13191325
});
13201326
};
13211327

test/server.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,9 +2505,10 @@ test('uncaughtException should handle thrown undefined literal', function(t) {
25052505
}
25062506
);
25072507

2508-
CLIENT.get('/foo', function(err, _, res) {
2508+
CLIENT.get('/foo', function(err, _, res, data) {
25092509
t.ok(err);
25102510
t.equal(res.statusCode, 500);
2511+
t.equal(data.message, '');
25112512
t.end();
25122513
});
25132514
});
@@ -2535,8 +2536,9 @@ test('uncaughtException should handle thrown Number', function(t) {
25352536
}
25362537
);
25372538

2538-
CLIENT.get('/foo', function(err, _, res) {
2539+
CLIENT.get('/foo', function(err, _, res, data) {
25392540
t.ok(err);
2541+
t.equal(data.message, '1');
25402542
t.equal(res.statusCode, 500);
25412543
t.end();
25422544
});

0 commit comments

Comments
 (0)