-
Notifications
You must be signed in to change notification settings - Fork 319
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
Handle exceptions during update so that they do not break subsequent updates #607
Conversation
…updates Fixes #262. Catch exceptions during update and ensure the element marks itself as finished updating. Note, this change also ensures the `updateComplete` promise is rejected if there's an exception during update.
src/lib/updating-element.ts
Outdated
let result = null; | ||
// Trap errors in updating so that element is not in a non-functional state. | ||
try { | ||
// Allow `performUpdate` to be asynchronous to enable scheduling of updates. |
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.
I don't immediately see how the comment relates to the code. What part of the statement allows performUpdate
to be async?
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.
Fixed.
src/lib/updating-element.ts
Outdated
// Note, this is to avoid delaying an additional microtask unless we need | ||
// to. | ||
if (result != null && | ||
typeof (result as PromiseLike<unknown>).then === 'function') { | ||
await result; | ||
try { |
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.
Can you merge with the previous try/catch block?
try {
const result = this.performUpdate();
if (result != null &&
typeof (result as PromiseLike<unknown>).then === 'function') {
await result;
}
} catch (e) {
this._markUpdated();
reject(e);
}
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, thanks.
@sorvell should we update any documentation for |
Added https://github.com/Polymer/lit-element/issues/608 to deal with the documentation. |
src/lib/updating-element.ts
Outdated
} | ||
} catch (e) { | ||
// Ensure subsequent updates are ok but reject this update. | ||
this._markUpdated(); |
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.
_markUpdated
is called in performUpdate
after update
and before firstUpdated
; if firstUpdated
or updated
throws, _markUpdated
is called twice. Is that ok? That would potentially throw out property queued changes before the error was thrown. Care?
A user error could occur in any of the following overridden spots:
After Clearing the update state means the element (a) marks itself as updated, allowing additional updates to be enqueued; and (b) clears any changed properties. It's important to note that If |
Offline review: catching |
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.
flipping approve bit off pending the new updates
* `shouldUpdate`/`update` are try/caught so that the element state is ok for further updates if an exception is thrown * `firstUpdated`/`updated` are not called if there's an update exception * `performUpdate` is try/caught only to reject the update promise. This is done to handle an override producing an exception. * a private version `_requestUpdate` is called in the property setter to avoid accessing the overridable `updateComplete` promise. * Added additional js doc comments.
src/lib/updating-element.ts
Outdated
// enable coordinating updates with a scheduler. Note, the result type is | ||
// checked to avoid delaying an additional microtask unless we need to. | ||
if (result != null && | ||
typeof (result as PromiseLike<unknown>).then === 'function') { |
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.
This code seems superfulous
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.
LGTM
Fixes #262.
Catch exceptions during update and ensure the element marks itself as finished updating.
Note, this change also ensures the
updateComplete
promise is rejected if there's an exception during update.