Uncaught promise in Auth.currentAuthenticatedUser breaks downstream Promise handling
See original GitHub issueDescribe the bug
After upgrading from @aws-amplify/auth": "1.2.13"
to "1.2.14"
, I seem to be hitting an uncaught Promise exception when calling Auth.currentAuthenticatedUser. It’s when the there is no current user currently logged in, i.e. LocalStorage is empty. I would expect a rejected promise and my catch to be hit, but I think a secondary promise is being rejected and uncaught, which is breaking the code.
It’s hard to debug as Angular wraps Promises. I’ve added the log to the bottom of this bug.
One thing to note, if I instead call Auth.currentPoolUser, the Promise is rejected, but my own Promise catch correctly handles it, which is what I would expect.
Also, I think the Auth.currentAuthenticatedUser
function is somewhat incorrect. it’s async, but it’s returning a Promise.
To Reproduce
Call Auth.currentAuthenticatedUser
when no user is logged in and expect a catch to be called.
Expected behavior It successfully calls the Promise.catch.
Desktop (please complete the following information):
- OS: Mac
- Browser: Chrome
- Version: 71
You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = ‘DEBUG’; in your app.
[DEBUG] 05:53.762 Amplify - component registered in amplify ƒ I18n() {
}
ConsoleLogger.js:78 [DEBUG] 05:53.807 Auth - Create Auth Instance
ConsoleLogger.js:88 [DEBUG] 05:53.808 Amplify - component registered in amplify AuthClass {userPool: null, _cognitoAuthClient: null, user: null, _gettingCredPromise: null, currentUserCredentials: ƒ}
core.js:16819 Angular is running in the development mode. Call enableProdMode() to enable the production mode.
ConsoleLogger.js:78 [DEBUG] 05:56.146 AuthClass - configure Auth
ConsoleLogger.js:88 [DEBUG] 05:56.149 Parser - parse config (3) [{…}, "to amplifyconfig", {…}]
ConsoleLogger.js:78 [DEBUG] 05:56.154 AuthClass - getting current authenticted user
ConsoleLogger.js:78 [DEBUG] 05:56.158 AuthClass - getting current authenticted user
ConsoleLogger.js:78 [DEBUG] 05:56.168 AuthClass - Getting current session
ConsoleLogger.js:78 [DEBUG] 05:56.174 AuthClass - cannot load federated user from auth storage
ConsoleLogger.js:78 [DEBUG] 05:56.174 AuthClass - get current authenticated userpool user
ConsoleLogger.js:78 [DEBUG] 05:56.175 AuthClass - cannot load federated user from auth storage
ConsoleLogger.js:78 [DEBUG] 05:56.176 AuthClass - get current authenticated userpool user
ConsoleLogger.js:78 [DEBUG] 05:56.176 AuthClass - Failed to get user from user pool
ConsoleLogger.js:78 [DEBUG] 05:56.177 AuthClass - Failed to get user from user pool
ConsoleLogger.js:78 [DEBUG] 05:56.178 AuthClass - Failed to get user from user pool
ConsoleLogger.js:88 [DEBUG] 05:56.180 AuthClass - Failed to get the current user No current user
ConsoleLogger.js:88 [DEBUG] 05:56.180 AuthClass - The user is not authenticated by the error No current user
ConsoleLogger.js:88 [DEBUG] 05:56.182 AuthClass - The user is not authenticated by the error No current user
core.js:15714 ERROR Error: Uncaught (in promise): No current user
at resolvePromise (zone.js:831)
at resolvePromise (zone.js:788)
at zone.js:892
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:17280)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
at drainMicroTaskQueue (zone.js:601)
defaultErrorLogger @ core.js:15714
push../node_modules/@angular/core/fesm5/core.js.ErrorHandler.handleError @ core.js:15762
next @ core.js:17761
schedulerFn @ core.js:13504
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub @ Subscriber.js:196
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next @ Subscriber.js:134
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next @ Subscriber.js:77
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next @ Subscriber.js:54
push../node_modules/rxjs/_esm5/internal/Subject.js.Subject.next @ Subject.js:47
push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit @ core.js:13488
(anonymous) @ core.js:17311
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:391
push../node_modules/zone.js/dist/zone.js.Zone.run @ zone.js:150
push../node_modules/@angular/core/fesm5/core.js.NgZone.runOutsideAngular @ core.js:17248
onHandleError @ core.js:17311
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.handleError @ zone.js:395
push../node_modules/zone.js/dist/zone.js.Zone.runGuarded @ zone.js:164
_loop_1 @ zone.js:694
api.microtaskDrainDone @ zone.js:703
drainMicroTaskQueue @ zone.js:608
Promise.then (async)
scheduleMicroTask @ zone.js:584
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.scheduleTask @ zone.js:413
push../node_modules/zone.js/dist/zone.js.Zone.scheduleTask @ zone.js:238
push../node_modules/zone.js/dist/zone.js.Zone.scheduleMicroTask @ zone.js:258
scheduleResolveOrReject @ zone.js:879
ZoneAwarePromise.then @ zone.js:1012
push../node_modules/@angular/core/fesm5/core.js.PlatformRef.bootstrapModule @ core.js:17793
./src/main.ts @ main.ts:12
__webpack_require__ @ bootstrap:83
0 @ main.ts:15
__webpack_require__ @ bootstrap:83
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.js:1
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
@powerful23 Hey man, I think I’ve tracked it down. You can ignore all the code from above. It’s hard to track where these Promises are actually being called from and I gave you a bad example.
I have an Angular HTTP Interceptor that retrieves the current auth token and attaches it to the header for every server request. It looks like this:
For some reason this is uncaught, and throwing the exception or Angular is not handling the exception as I would expect. I’m not sure how Angular manages interceptors that reject Promises/Observables, but it surely doesn’t seem to play nice if you don’t reject the right thing.
Adding a
catchError(() => of(undefined))
seems to play well here because:Unfortunately, I’m not exactly sure what’s different between Auth 1.2.13 & 1.2.14 that would cause this. But I’m going to close this and hope you didn’t spend too much time looking at it.
This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server
*-help
channels or Discussions for those types of questions.