Skip to content

Commit a24ab56

Browse files
cjihrigtargos
authored andcommitted
src: allow fatal exceptions to be enhanced
This commit allows fatal exceptions to be enhanced so that exceptions thrown from an unhandledException handler have the stack attached properly. PR-URL: #28562 Fixes: #28550 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 4cb0fc3 commit a24ab56

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/node_errors.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,11 @@ TryCatchScope::~TryCatchScope() {
448448
HandleScope scope(env_->isolate());
449449
Local<v8::Value> exception = Exception();
450450
Local<v8::Message> message = Message();
451+
EnhanceFatalException enhance = CanContinue() ?
452+
EnhanceFatalException::kEnhance : EnhanceFatalException::kDontEnhance;
451453
if (message.IsEmpty())
452454
message = Exception::CreateMessage(env_->isolate(), exception);
453-
ReportFatalException(
454-
env_, exception, message, EnhanceFatalException::kDontEnhance);
455+
ReportFatalException(env_, exception, message, enhance);
455456
env_->Exit(7);
456457
}
457458
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
require('../common');
3+
4+
if (process.argv[2] === 'child') {
5+
process.on('uncaughtException', (err) => {
6+
err.rethrow = true;
7+
throw err;
8+
});
9+
10+
function throwException() {
11+
throw new Error('boom');
12+
}
13+
14+
throwException();
15+
} else {
16+
const assert = require('assert');
17+
const { spawnSync } = require('child_process');
18+
const result = spawnSync(process.execPath, [__filename, 'child']);
19+
20+
assert.strictEqual(result.status, 7);
21+
assert.strictEqual(result.signal, null);
22+
assert.strictEqual(result.stdout.toString().trim(), '');
23+
// Verify that the error was thrown and that the stack was preserved.
24+
const stderr = result.stderr.toString();
25+
assert(/Error: boom/.test(stderr));
26+
assert(/at throwException/.test(stderr));
27+
assert(/rethrow: true/.test(stderr));
28+
}

0 commit comments

Comments
 (0)