Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create single path for handling errors from the protocol #977

Merged
merged 2 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions lighthouse-core/gather/connections/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,34 +95,41 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absorbing errors we don't care...

it's only the DOM.disable atm.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* about and throwing on the rest.
*
* Currently the only error ignored is from defensive calls of `DOM.disable`
* when already disabled.
* @param {{message: string}} error
* @param {{resolve: function(*), reject: function(*), method: string}} callback
* @param {string} method Protocol method that received the error response.
* @throws {Error}
* @protected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this stay underscored if it's meant to be @protected?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a @throws?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, no underscore for @protected (not that closure is looking at this, really only annotating to keep in the style of the rest of the file)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a @throws

done

*/
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more just an aside here, i've had to do this a couple times in the past too and always thought there had to be a better way i was missing, i guess not? haha

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha, yeah. The docs on lastError are really unhelpful. Not only is message marked optional, there's no hint of what the format of the error is, it's just always JSON when I try it...

} 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing chrome.debugger.sendCommand doesn't return a promise? otherwise we could follow the earlier pattern

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, only callback

return resolve(this.handleRawError(error, command));
} catch (err) {
return reject(err);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is a little awkward (as noted) but I think I prefer this flavor to what we had here before.


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 @@ -155,7 +155,10 @@ class Driver {
} else {
resolve(result.result.value);
}
}).catch(reject);
}).catch(err => {
clearTimeout(asyncTimeout);
reject(err);
});
});
}

Expand Down