Skip to content

Commit

Permalink
fix(connection): gracefully handle missing method (#2351)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored and paulirish committed Jun 5, 2017
1 parent d0a72c3 commit 89798d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
16 changes: 11 additions & 5 deletions lighthouse-core/gather/connections/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Connection {
const object = JSON.parse(message);
// Remote debugging protocol is JSON RPC 2.0 compiant. In terms of that transport,
// responses to the commands carry "id" property, while notifications do not.
if (object.id) {
if (this._callbacks.has(object.id)) {
const callback = this._callbacks.get(object.id);
this._callbacks.delete(object.id);

Expand All @@ -108,11 +108,17 @@ class Connection {
{method: callback.method, params: object.result}, 'verbose');
return object.result;
}));
} else if (object.id) {
// In DevTools we receive responses to commands we did not send which we cannot act on, so we
// just log these occurrences.
const error = object.error && object.error.message;
log.formatProtocol(`disowned method <= browser ${error ? 'ERR' : 'OK'}`,
{method: object.method, params: error || object.result}, 'verbose');
} else {
log.formatProtocol('<= event',
{method: object.method, params: object.params}, 'verbose');
this.emitNotification(object.method, object.params);
}

log.formatProtocol('<= event',
{method: object.method, params: object.params}, 'verbose');
this.emitNotification(object.method, object.params);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions lighthouse-core/lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ class Log {
*/
static formatProtocol(prefix, data, level) {
const columns = (!process || process.browser) ? Infinity : process.stdout.columns;
const maxLength = columns - data.method.length - prefix.length - loggingBufferColumns;
const method = data.method || '?????';
const maxLength = columns - method.length - prefix.length - loggingBufferColumns;
// IO.read blacklisted here to avoid logging megabytes of trace data
const snippet = (data.params && data.method !== 'IO.read') ?
const snippet = (data.params && method !== 'IO.read') ?
JSON.stringify(data.params).substr(0, maxLength) : '';
Log._logToStdErr(`${prefix}:${level || ''}`, [data.method, snippet]);
Log._logToStdErr(`${prefix}:${level || ''}`, [method, snippet]);
}

static log(title, ...args) {
Expand Down

0 comments on commit 89798d1

Please sign in to comment.