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

Conversation

brendankenny
Copy link
Member

starts addressing #976
also gives slightly more context for errors like #966 (as well as a real stack trace)

Moves the extension and the raw connection to use the same error handler for messages coming back from the debugging protocol, as well as normalizing the error object fed into it. This should aid debugging. Happens to unify catching of DOM.disable error, instead of in separate spots for extension and CLI.

Also removes old result.wasThrown from extension sendCommand, which as discussed in #976 was doing nothing but confusing us.

@brendankenny
Copy link
Member Author

brendankenny commented Nov 19, 2016

to be clear, this does not handle the main part of #976, dealing with exceptionDetails, but a simpler path for protocol errors (as opposed to errors from e.g. code running in the browser via the protocol) should make that easier

error = error || {message: 'Unknown debugger protocol error.'};

const callback = {
resolve,
Copy link
Collaborator

Choose a reason for hiding this comment

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

the use of new Promise now feels pretty weird here since we're basically passing both resolve and reject to another function, thoughts on Promise.try or the stop-gap Promise.resolve().then instead and just having handleRawError return its own promise?

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, I like it. This is kind of awkward because of the raw connection setup, but that should work for both

Copy link
Member

Choose a reason for hiding this comment

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

I haven't run into (too many) issues using the Promise.resolve().then pattern Patrick suggested above, so a 👍 to switching over to that and letting handleRawError do it's bizness.

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, I wasn't going to do that part :) I'll consider it, but we use resolve(result); farther down in that function. It is a simplistic use of a promise, but I don't really mind resolve vs return, and exceptions are treated the same way. I'm sure others feel otherwise :)

Copy link
Member Author

Choose a reason for hiding this comment

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

incidentally, this is one more reason we need to always avoid Promise.alling any debugger protocol code since any refactor like this might introduce ticks between steps, even in driver functions that only run a single command :)

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, the more I think about it, the more I like this pattern. I had avoided it in the past just because the `Promise.resolve() part feels like a waste (and the extra tick from it can be an issue for reasoning about things running in parallel), but the consistency in code is pretty nice. I'd be in favor of adopting this pattern in the future for functions returning a promise but also needing to swallow sync exceptions.

We need to be careful converting old code, though, because I know there are a few places we've written code explicitly assuming the executor will be executed synchronously (which is actually one more point in the consistency column if we always assume this pattern)

Copy link
Member

Choose a reason for hiding this comment

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

I'd be in favor of adopting this pattern in the future for functions returning a promise but also needing to swallow sync exceptions.

👏

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...

@brendankenny
Copy link
Member Author

updated, PTAL
@addyosmani @patrickhulce

error = error || {message: 'Unknown debugger protocol error.'};

// 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

Copy link
Contributor

@ebidel ebidel left a comment

Choose a reason for hiding this comment

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

LGTM with the suggestions

* about and throwing on the rest.
* @param {{message: string}} error
* @param {string} method Protocol method that received the error response.
* @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

* @param {{error: {message: string}}} object
* @param {{reject: function(*), method: string}} callback
* @private
* 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

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.

@addyosmani
Copy link
Member

Waiting for travis to complete a run on your last set of commits, but looks like it's all good to go.

@addyosmani addyosmani merged commit 74a7665 into master Nov 23, 2016
@brendankenny brendankenny deleted the protocolerrors branch November 23, 2016 00:56
andrewrota pushed a commit to andrewrota/lighthouse that referenced this pull request Jan 13, 2017
…e#977)

* create single path for handling errors from the protocol

* make handleRawError synchronous
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants