-
Notifications
You must be signed in to change notification settings - Fork 823
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
Throwing error instead of re registering sync on failure #1155
Conversation
33798f5
to
6c79afd
Compare
Changes Unknown when pulling 2e6c392 on bgSyncRetryFix into ** on v3**. |
if (failedRequests.length) { | ||
await Promise.all(failedRequests.map((storableRequest) => { | ||
return this._queueStore.addEntry(storableRequest); | ||
})); | ||
|
||
await this._registerSync(); | ||
return Promise.reject(failedRequests); |
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.
Nit: can we throw here instead of returning a rejected promise? I think it's better to use async function patterns when inside an async function (rather that promise patterns).
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.
so throw new Error(failedRequests);
?
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 was thinking just:
throw failedRequests;
In JS you can throw any value.
cc: @gauntface and @jeffposnick to see if they have thoughts on this.
In general I think we should stick to the pattern that async function use await
, try
, catch
, and throw
, and regular functions use resolve
and reject
, though I guess this may be a bit of a special case...
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.
Does anything end up consuming the rejected promise that's returned by this function? Or is the fact that it's a rejected promise all that matters, with the rejection value ignored?
If the value is ignored, then I'd rather see
throw new Error('<some meaningful message>');
so that a developer reading the source has a bit more context. And if at some point in the future logging is added in, it will end up with a meaningful log message.
If the value isn't ignored, and it needs to be that array, then using Promise.reject()
seems fine to me, but I'd ask that you add in a comment explaining what's going on.
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.
yeah nothing really consumes it.
- i was thinking something like
throw new Error('X number of requests failed');
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.
Please use a WorkboxError.
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
return; | ||
} | ||
|
||
return Promise.reject('should have exit from catch'); |
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.
Same as above, can we throw here instead of returning a rejected promise?
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
expect(self.registration.sync.register.calledOnce).to.be.true; | ||
expect(self.registration.sync.register.calledWith( | ||
'workbox-background-sync:foo')).to.be.true; | ||
return Promise.reject('should have exit from catch'); |
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.
Same as above, can we throw here instead of returning a rejected promise?
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
// If any requests failed, put the failed requests back in the queue | ||
// and register for another sync. | ||
// and reject promise. |
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.
Maybe reword this to: "and rethrow the failed requests"?
Note: this comment I'm less concerned about changing...
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
68fb011
to
2e6c392
Compare
@philipwalton correct me if I am wrong, but |
Plain |
As Jeff pointed out - we should be using useful information to explain what is happening if a developer is viewing this. The scenario you described sounds like an expected and valid scenario (i.e. the request failed to work - no worries, we'll still have the sync set up). For this - use a WorkboxError with a valid string to explain what has happened. This means the developer will know the error is from Workbox (because it's a |
9e6125f
to
6c48088
Compare
Changes Unknown when pulling 6c48088 on bgSyncRetryFix into ** on v3**. |
Changes Unknown when pulling 6c48088 on bgSyncRetryFix into ** on v3**. |
return; | ||
} | ||
|
||
throw new Error('should have exit from catch'); |
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.
Could you use the helper: https://github.com/GoogleChrome/workbox/blob/v3/infra/testing/expectError.js
This will check the error is a WorkboxError with an expected code and it returns a promise, so you can await for the response and check the object store entries after.
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
try { | ||
await queue.replayRequests(); // The second request should fail. | ||
} catch (error) { | ||
expect(error).to.be.instanceof(WorkboxError); |
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.
Please use expectError helper here as well.
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
expect(self.registration.sync.register.calledOnce).to.be.true; | ||
expect(self.registration.sync.register.calledWith( | ||
'workbox-background-sync:foo')).to.be.true; | ||
throw new Error('should have exit from catch'); |
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 wont be needed after switching to expectError().
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
@@ -376,7 +382,13 @@ describe(`[workbox-background-sync] Queue`, function() { | |||
|
|||
await queue.addRequest(new Request('/three')); | |||
await queue.addRequest(new Request('/four')); | |||
await queue.replayRequests(); | |||
try { | |||
await queue.replayRequests(); |
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.
Please use expectError here and make sure the error is what you expect.
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
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.
Could you switch to expectError() helper throughout the tests to ensure everything runs as we expect after future changes.
PR-Bot Size PluginChanged File Sizes
New FilesNo new files have been added. All File SizesView Table
Workbox Aggregate Size Plugin☠️ WARNING ☠️We are using 152% of our max size budget. Total Size: 22.27KB Gzipped: 8.9KB |
Changes Unknown when pulling 274bb31 on bgSyncRetryFix into ** on v3**. |
R: @philipwalton
CC: @jeffposnick @addyosmani @gauntface
Fixes #1145
Currently, on any failures in queue replay it results in re-registering of sync queue tag.
This causes infinite loop in case of
offline
from devtools + IIRC will not re-play the failed requests as this will re-register the same tag and also resolve the promise insideevent.waitUntil
ofsync
event. Hence telling the browser that we've successfully done everything related to this tag.Instead to indicate any failure it is require that the promise passed to
event.waitUntil
should be rejected. ref: https://github.com/WICG/BackgroundSync/blob/master/explainer.md#the-api