Skip to content

Commit

Permalink
inspector: log exceptions in message handlers
Browse files Browse the repository at this point in the history
Fixes: #14965
PR-URL: #14980
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
  • Loading branch information
Eugene Ostroukhov authored and MylesBorins committed Sep 12, 2017
1 parent ea86cb5 commit 105acf4
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 105acf4

Please sign in to comment.