Skip to content

zone.js promises returning Unhandled Promise Rejection error even with catch blocks #31680

Closed

Description

@utsavkapoor

I am working on an angular environment with zone.js so all the native es6 promises have been over-written by zone-aware promises. I have 2 functions called save() and onSaveCb()

save() calls a function called validate() which gets the error-codes of any of the saved objects already present in the file so it is done in sync.

if hasError() returns true we return Promise.reject() from save() otherwise we send a save request to the backend.

Now, onSaveCb() function is an async/await function which has a try-catch block around save().

Problem:

When Promise is rejected from save() function, I see console log error:

zone.js:682 Unhandled Promise rejection: ; Zone: ; Task: onClick Event ; Value: Error:

I tried a bunch of things with wrapping the function save in a promise and then rejecting it, but i still see the same error

function save() {
   let errorCode = this.validate(); // This is a sync call
   if(errorCode !== 0) { return Promise.reject(errorCode);}
   return https(url).then(function() {return true});
} 

function async onSaveCb() {
   try{
     let response = await this.save();
     alertUI(response);
     } catch (response) {
       alertUI(response);
     }
}

I am not sure why I am getting this message when I have clearly written error handling in the catch block. Any help here would be helpful

Activity

  1. JiaLiPassion commented on Jun 19, 2019

    @JiaLiPassion
    Contributor

    @utsavkapoor thanks for posting the issue, I will check it.

  2. JiaLiPassion commented on Jul 19, 2019

    @JiaLiPassion
    Contributor

    @utsavkapoor, could you provide a sample repo to reproduce, thanks!

  3. self-assigned this
    on Jul 20, 2019
  4. added this to the needsTriage milestone on Jul 20, 2019
  5. Kanti commented on Sep 3, 2019

    @Kanti

    copy this inside an empty chrome window.
    or open it here: https://codepen.io/kanti/pen/yLBPLGB?editors=0011
    or here: https://jsfiddle.net/iKanti/b67zu4pq/1/

    Without zone.js it doesn't throw an Unhandled Promise rejection.
    But with zone.js it throws an Unhandled Promise rejection.

    function x() {
      return Promise.reject(new Error('Expired Token'))
    }
    (async function() {
      await x();
    })().catch(console.warn);
    // the above code will print no:
    // 'Unhandled Promise rejection: Expired Token ; Zone: <root> ; Task: null ; Value: Error: Expired Token'
    // but the code below will:
    import('https://unpkg.com/zone.js').then(() => {
      (async function() {
        await x();
      })().catch(console.warn);
    });
  6. JiaLiPassion commented on Sep 5, 2019

    @JiaLiPassion
    Contributor

    @Kanti, sure, I have fixed this one, but it break some cases inside Google, so I am working on to provide an option flag to enable/disable this uncaught promise error feature of zone.js.

  7. thw0rted commented on Jan 29, 2020

    @thw0rted

    Is there any update on this issue? I'm seeing warnings about promises I actually catch as well (using promise.then(x => { }, err => { })). I don't want to totally disable Zone's uncaught promise detection feature -- it would be great if it worked! -- but I'd like it to stop spuriously telling me I didn't catch something that is in fact caught.

  8. JiaLiPassion commented on May 28, 2020

    @JiaLiPassion
    Contributor

    this issue should be fixed by this PR #35873

  9. angular-automatic-lock-bot commented on Jun 28, 2020

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  10. locked and limited conversation to collaborators on Jun 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: zonesIssues related to zone.js

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions