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

Prevent UnhandledPromiseError when restoring the session #2363

Merged

Conversation

swelham
Copy link
Contributor

@swelham swelham commented Feb 23, 2022

It's possible for an UnhandledPromiseError to raise in the background when restoring the session. This doesn't raise to the caller of the library but does occur as a caught exception in the browser, which in turn can be picked up by error tracking code such as sentry.

This fixes #2358 which has more specific detail around the issue and how to reproduce it.

@BobrImperator
Copy link
Collaborator

Hi, thanks for working on this!
Let me just quickly say that I didn't ignore your issue, I simply forgot to reply 🙈

I don't exactly think this is valid, it feels like we're just silencing an actual problem in the code.
It seems like sentry is hooking up to RSVP.on('error') directly for some reason, which I'm not exactly sure is correct, but if some error is thrown to there we should fix it instead.

It'd be nice to reproduce it and cover with a test, I could try taking a look at it.

@swelham
Copy link
Contributor Author

swelham commented Feb 25, 2022

Hey, no problem at all.

I tried writing a test for this but I was unable to reproduce it. I think it's because the try/catch will swallow any error thrown by the promise. Though I didn't think to try using RSVP.on('error'), maybe that would work? If we used the .catch() suggested in this PR would we still need the try/catch? Without that I believe it would be more testable.

The error that is being thrown comes from the use of this const https://github.com/simplabs/ember-simple-auth/blob/master/packages/ember-simple-auth/addon/internal-session.js#L69 when it's passed as the callback to the promise, which I think is a valid use of that. However, because the promise isn't directly handled by the try/catch it's still regeristing an exception.

@BobrImperator
Copy link
Collaborator

Here's a reproduction of what might be happening here https://ember-twiddle.com/939eb4a8aec7f6b188cfc281140191e6?openFiles=controllers.application%5C.js%2C.
Interestingly Native promise doesn't throw any errors when in try catch block, but RSVP promise does throw.

I think we could move forward with this PR but it'd be better to drop the try catch block I think.

 setup() {
    this._setupIsCalled = true;
    this._setupHandlers();
    
    return this.session.restore().catch(() => {
          // If it raises an error then it means that restore didn't find any restorable state.
    });
  }

@swelham swelham force-pushed the fix-unhandled-promise-during-restore branch from 8c8784f to d54f27e Compare March 14, 2022 15:06
@swelham
Copy link
Contributor Author

swelham commented Mar 14, 2022

Thanks for the detailed response and twiddle.

I have updated the PR with the suggested code changes.

@marcoow
Copy link
Member

marcoow commented Mar 14, 2022

I don't quite understand this tbh – there shouldn't be a difference between try/catch and .catch(… – if there is it seems that would be because your error tracking might hook into the wrong event or so?

Admittedly it wasn't a great API choice to have the session's restore method raise an error if there's no restorable state in the session data but I wouldn't want to go back to raw promise APIs unless we absolutely have to…

@BobrImperator
Copy link
Collaborator

@marcoow RSVP promises seem to still emit errors to RSVP.on('error') which is a hook that sentry uses (for whatever reason). Also ultimately this is a regression on ESA's part as well, since the old code was actually using .catch method while currently it uses try catch instead.

ESA used to pass a function that ran beforeModel hook inside a promise rejection handler.
https://github.com/simplabs/ember-simple-auth/blob/master/packages/ember-simple-auth/addon/initializers/setup-session-restoration.js#L38

@marcoow
Copy link
Member

marcoow commented Mar 15, 2022

Also ultimately this is a regression on ESA's part as well, since the old code was actually using .catch method while currently it uses try catch instead.

Hm, yeah – I think that's a reasonable argument for changing this 🤔

@BobrImperator
Copy link
Collaborator

It seems like one test is failing now because the stubbed restore has a wrong return value 😞

It seems it should be sinon.stub(session, 'restore').returns(RSVP.reject())

It's possible for an `UnhandledPromiseError` to raise when restoring the session. This doesn't raise to the caller of the library but does occur as a caught exception in the browser, which in turn can be picked up by error tracking code such as sentry.

This fixes mainmatter#2358 which has more specific detail around the issue and how to reproduce it.
@swelham swelham force-pushed the fix-unhandled-promise-during-restore branch from d54f27e to 27f92e0 Compare March 15, 2022 14:40
@swelham
Copy link
Contributor Author

swelham commented Mar 15, 2022

Ah, sorry I should have caught that before pushing that change.

Test should now be passing on CI.

Copy link
Collaborator

@BobrImperator BobrImperator left a comment

Choose a reason for hiding this comment

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

Thank you!

@BobrImperator BobrImperator merged commit ed981ff into mainmatter:master Mar 15, 2022
@BobrImperator BobrImperator added bug and removed bug labels Mar 15, 2022
@BobrImperator
Copy link
Collaborator

@swelham released https://github.com/simplabs/ember-simple-auth/blob/master/CHANGELOG.md

@swelham
Copy link
Contributor Author

swelham commented Mar 15, 2022

Thanks @BobrImperator 👍

@swelham swelham deleted the fix-unhandled-promise-during-restore branch March 15, 2022 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unhandled Promise error detected when using the manual session.setup function
3 participants