inspector: log exceptions in message handlers

Fixes: https://github.com/nodejs/node/issues/14965
PR-URL: https://github.com/nodejs/node/pull/14980
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
This commit is contained in:
Eugene Ostroukhov 2017-08-22 09:46:33 -07:00
parent 7540821fb5
commit 9bae3eacc6
2 changed files with 57 additions and 19 deletions

View File

@ -29,14 +29,18 @@ class Session extends EventEmitter {
[onMessageSymbol](message) { [onMessageSymbol](message) {
const parsed = JSON.parse(message); const parsed = JSON.parse(message);
if (parsed.id) { try {
const callback = this[messageCallbacksSymbol].get(parsed.id); if (parsed.id) {
this[messageCallbacksSymbol].delete(parsed.id); const callback = this[messageCallbacksSymbol].get(parsed.id);
if (callback) this[messageCallbacksSymbol].delete(parsed.id);
callback(parsed.error || null, parsed.result || null); if (callback)
} else { callback(parsed.error || null, parsed.result || null);
this.emit(parsed.method, parsed); } else {
this.emit('inspectorNotification', parsed); this.emit(parsed.method, parsed);
this.emit('inspectorNotification', parsed);
}
} catch (error) {
process.emitWarning(error);
} }
} }

View File

@ -33,14 +33,25 @@ function debuggerPausedCallback(session, notification) {
checkScope(session, scopeId); 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 // There is a deliberate exception in the callback
const session = new inspector.Session(); const session = new inspector.Session();
session.connect(); session.connect();
const error = new Error('We expect this'); const error = new Error('We expect this');
assert.throws(() => { console.log('Expecting warning to be emitted');
session.post('Console.enable', () => { throw error; }); const promise = new Promise(waitForWarningSkipAsyncStackTraces);
}, (e) => e === error); session.post('Console.enable', () => { throw error; });
assert.strictEqual(await promise, error);
session.disconnect(); session.disconnect();
} }
@ -97,10 +108,33 @@ function testSampleDebugSession() {
assert.throws(() => session.post('Debugger.enable'), (e) => !!e); assert.throws(() => session.post('Debugger.enable'), (e) => !!e);
} }
testNoCrashWithExceptionInCallback(); async function testNoCrashConsoleLogBeforeThrow() {
testSampleDebugSession(); const session = new inspector.Session();
let breakpointHit = false; session.connect();
scopeCallback = () => (breakpointHit = true); let attempt = 1;
debuggedFunction(); process.on('warning', common.mustCall(3));
assert.strictEqual(breakpointHit, false); session.on('inspectorNotification', () => {
testSampleDebugSession(); 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();