Skip to content

Commit 5cb81bd

Browse files
Eugene OstroukhovMylesBorins
Eugene Ostroukhov
authored andcommitted
inspector: log exceptions in message handlers
Fixes: #14965 PR-URL: #14980 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent ca3c4a0 commit 5cb81bd

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

lib/inspector.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ class Session extends EventEmitter {
2929

3030
[onMessageSymbol](message) {
3131
const parsed = JSON.parse(message);
32-
if (parsed.id) {
33-
const callback = this[messageCallbacksSymbol].get(parsed.id);
34-
this[messageCallbacksSymbol].delete(parsed.id);
35-
if (callback)
36-
callback(parsed.error || null, parsed.result || null);
37-
} else {
38-
this.emit(parsed.method, parsed);
39-
this.emit('inspectorNotification', parsed);
32+
try {
33+
if (parsed.id) {
34+
const callback = this[messageCallbacksSymbol].get(parsed.id);
35+
this[messageCallbacksSymbol].delete(parsed.id);
36+
if (callback)
37+
callback(parsed.error || null, parsed.result || null);
38+
} else {
39+
this.emit(parsed.method, parsed);
40+
this.emit('inspectorNotification', parsed);
41+
}
42+
} catch (error) {
43+
process.emitWarning(error);
4044
}
4145
}
4246

test/inspector/test-bindings.js

+45-11
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,25 @@ function debuggerPausedCallback(session, notification) {
3333
checkScope(session, scopeId);
3434
}
3535

36-
function testNoCrashWithExceptionInCallback() {
36+
function waitForWarningSkipAsyncStackTraces(resolve) {
37+
process.once('warning', function(warning) {
38+
if (warning.code === 'INSPECTOR_ASYNC_STACK_TRACES_NOT_AVAILABLE') {
39+
waitForWarningSkipAsyncStackTraces(resolve);
40+
} else {
41+
resolve(warning);
42+
}
43+
});
44+
}
45+
46+
async function testNoCrashWithExceptionInCallback() {
3747
// There is a deliberate exception in the callback
3848
const session = new inspector.Session();
3949
session.connect();
4050
const error = new Error('We expect this');
41-
assert.throws(() => {
42-
session.post('Console.enable', () => { throw error; });
43-
}, (e) => e === error);
51+
console.log('Expecting warning to be emitted');
52+
const promise = new Promise(waitForWarningSkipAsyncStackTraces);
53+
session.post('Console.enable', () => { throw error; });
54+
assert.strictEqual(await promise, error);
4455
session.disconnect();
4556
}
4657

@@ -97,10 +108,33 @@ function testSampleDebugSession() {
97108
assert.throws(() => session.post('Debugger.enable'), (e) => !!e);
98109
}
99110

100-
testNoCrashWithExceptionInCallback();
101-
testSampleDebugSession();
102-
let breakpointHit = false;
103-
scopeCallback = () => (breakpointHit = true);
104-
debuggedFunction();
105-
assert.strictEqual(breakpointHit, false);
106-
testSampleDebugSession();
111+
async function testNoCrashConsoleLogBeforeThrow() {
112+
const session = new inspector.Session();
113+
session.connect();
114+
let attempt = 1;
115+
process.on('warning', common.mustCall(3));
116+
session.on('inspectorNotification', () => {
117+
if (attempt++ > 3)
118+
return;
119+
console.log('console.log in handler');
120+
throw new Error('Exception in handler');
121+
});
122+
session.post('Runtime.enable');
123+
console.log('Did not crash');
124+
session.disconnect();
125+
}
126+
127+
common.crashOnUnhandledRejection();
128+
129+
async function doTests() {
130+
await testNoCrashWithExceptionInCallback();
131+
testSampleDebugSession();
132+
let breakpointHit = false;
133+
scopeCallback = () => (breakpointHit = true);
134+
debuggedFunction();
135+
assert.strictEqual(breakpointHit, false);
136+
testSampleDebugSession();
137+
await testNoCrashConsoleLogBeforeThrow();
138+
}
139+
140+
doTests();

0 commit comments

Comments
 (0)