Skip to content

Commit

Permalink
inspector: log exceptions in message handlers
Browse files Browse the repository at this point in the history
Fixes: nodejs#14965
PR-URL: nodejs#14980
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
  • Loading branch information
Eugene Ostroukhov authored and addaleax committed Sep 13, 2017
1 parent c6f3374 commit d19377c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
20 changes: 12 additions & 8 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ class Session extends EventEmitter {

[onMessageSymbol](message) {
const parsed = JSON.parse(message);
if (parsed.id) {
const callback = this[messageCallbacksSymbol].get(parsed.id);
this[messageCallbacksSymbol].delete(parsed.id);
if (callback)
callback(parsed.error || null, parsed.result || null);
} else {
this.emit(parsed.method, parsed);
this.emit('inspectorNotification', parsed);
try {
if (parsed.id) {
const callback = this[messageCallbacksSymbol].get(parsed.id);
this[messageCallbacksSymbol].delete(parsed.id);
if (callback)
callback(parsed.error || null, parsed.result || null);
} else {
this.emit(parsed.method, parsed);
this.emit('inspectorNotification', parsed);
}
} catch (error) {
process.emitWarning(error);
}
}

Expand Down
56 changes: 45 additions & 11 deletions test/inspector/test-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,25 @@ function debuggerPausedCallback(session, notification) {
checkScope(session, scopeId);
}

function testNoCrashWithExceptionInCallback() {
function waitForWarningSkipAsyncStackTraces(resolve) {
process.once('warning', function(warning) {
if (warning.code === 'INSPECTOR_ASYNC_STACK_TRACES_NOT_AVAILABLE') {
waitForWarningSkipAsyncStackTraces(resolve);
} else {
resolve(warning);
}
});
}

async function testNoCrashWithExceptionInCallback() {
// There is a deliberate exception in the callback
const session = new inspector.Session();
session.connect();
const error = new Error('We expect this');
assert.throws(() => {
session.post('Console.enable', () => { throw error; });
}, (e) => e === error);
console.log('Expecting warning to be emitted');
const promise = new Promise(waitForWarningSkipAsyncStackTraces);
session.post('Console.enable', () => { throw error; });
assert.strictEqual(await promise, error);
session.disconnect();
}

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

testNoCrashWithExceptionInCallback();
testSampleDebugSession();
let breakpointHit = false;
scopeCallback = () => (breakpointHit = true);
debuggedFunction();
assert.strictEqual(breakpointHit, false);
testSampleDebugSession();
async function testNoCrashConsoleLogBeforeThrow() {
const session = new inspector.Session();
session.connect();
let attempt = 1;
process.on('warning', common.mustCall(3));
session.on('inspectorNotification', () => {
if (attempt++ > 3)
return;
console.log('console.log in handler');
throw new Error('Exception in handler');
});
session.post('Runtime.enable');
console.log('Did not crash');
session.disconnect();
}

common.crashOnUnhandledRejection();

async function doTests() {
await testNoCrashWithExceptionInCallback();
testSampleDebugSession();
let breakpointHit = false;
scopeCallback = () => (breakpointHit = true);
debuggedFunction();
assert.strictEqual(breakpointHit, false);
testSampleDebugSession();
await testNoCrashConsoleLogBeforeThrow();
}

doTests();

0 comments on commit d19377c

Please sign in to comment.