-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
* 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this stay underscored if it's meant to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, no underscore for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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}`); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha, yeah. The docs on |
||
} 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done