Skip to content

Commit

Permalink
make handleRawError synchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Nov 21, 2016
1 parent b4b5e11 commit 835ce37
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
25 changes: 14 additions & 11 deletions lighthouse-core/gather/connections/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,34 +95,37 @@ class Connection {
const callback = this._callbacks.get(object.id);
this._callbacks.delete(object.id);

if (object.error) {
return this.handleRawError(object.error, callback);
}
// handleRawError returns or throws synchronously; wrap to put into promise chain.
return callback.resolve(Promise.resolve().then(_ => {
if (object.error) {
return this.handleRawError(object.error, callback.method);
}

log.formatProtocol('method <= browser OK',
log.formatProtocol('method <= browser OK',
{method: callback.method, params: object.result}, 'verbose');
callback.resolve(object.result);
return;
return object.result;
}));
}
log.formatProtocol('<= event',
{method: object.method, params: object.params}, 'verbose');
this.emitNotification(object.method, object.params);
}

/**
* Handles error responses from the protocol, absorbing errors we don't care
* about and throwing on the rest.
* @param {{message: string}} error
* @param {{resolve: function(*), reject: function(*), method: string}} callback
* @param {string} method Protocol method that received the error response.
* @protected
*/
handleRawError(error, callback) {
handleRawError(error, method) {
// We proactively disable the DOM domain. Ignore any errors.
if (error.message && error.message.includes('DOM agent hasn\'t been enabled')) {
callback.resolve();
return;
}

log.formatProtocol('method <= browser ERR', {method: callback.method}, 'error');
callback.reject(new Error(`Protocol error (${callback.method}): ${error.message}`));
log.formatProtocol('method <= browser ERR', {method}, 'error');
throw new Error(`Protocol error (${method}): ${error.message}`);
}

/**
Expand Down
14 changes: 8 additions & 6 deletions lighthouse-core/gather/connections/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,21 @@ class ExtensionConnection extends Connection {

chrome.debugger.sendCommand({tabId: this._tabId}, command, params, result => {
if (chrome.runtime.lastError) {
// The error from the extension has a `message` property that is the
// stringified version of the actual protocol error object.
const message = chrome.runtime.lastError.message;
let error;
try {
error = JSON.parse(message);
} catch (e) {}
error = error || {message: 'Unknown debugger protocol error.'};

const callback = {
resolve,
reject,
method: command
};
return this.handleRawError(error, callback);
// handleRawError returns or throws synchronously, so try/catch awkwardly.
try {
return resolve(this.handleRawError(error, command));
} catch (err) {
return reject(err);
}
}

log.formatProtocol('method <= browser OK', {method: command, params: result}, 'verbose');
Expand Down
5 changes: 4 additions & 1 deletion lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ class Driver {
}).then(result => {
clearTimeout(asyncTimeout);
resolve(result.result.value);
}).catch(reject);
}).catch(err => {
clearTimeout(asyncTimeout);
reject(err);
});
});
}

Expand Down

0 comments on commit 835ce37

Please sign in to comment.