Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Promise rejection logs two error messages in the console #806

Closed
hmdhk opened this issue Jun 9, 2017 · 7 comments
Closed

Promise rejection logs two error messages in the console #806

hmdhk opened this issue Jun 9, 2017 · 7 comments

Comments

@hmdhk
Copy link
Contributor

hmdhk commented Jun 9, 2017

What is the reason for logging two error messages in the console when a promise is rejected?
Can it be merged together to avoid cluttering the console?

@JiaLiPassion
Copy link
Collaborator

@jahtalab, sure, it should only logging one message, I will check it.

@hmdhk
Copy link
Contributor Author

hmdhk commented Jun 9, 2017

@JiaLiPassion ,

This is the piece of code that does the logging:

https://github.com/angular/zone.js/blob/master/lib/common/promise.ts#L21

  api.onUnhandledError = (e: any) => {
    if (api.showUncaughtError()) {
      const rejection = e && e.rejection;
      if (rejection) {
        console.error(
            'Unhandled Promise rejection:',
            rejection instanceof Error ? rejection.message : rejection, '; Zone:',
            (<Zone>e.zone).name, '; Task:', e.task && (<Task>e.task).source, '; Value:', rejection,
            rejection instanceof Error ? rejection.stack : undefined);
      }
      console.error(e);
    }
  };

I'm guessing that the first console.error is for debugging of zone.js, is that right? Does it have another purpose?

@JiaLiPassion
Copy link
Collaborator

@jahtalab , it is not a debug code, the first console.error will print the detail information about the real error, if rejection not exists, wi print error itself, so it should be if/else. I will fix it. Thanks!

JiaLiPassion added a commit to JiaLiPassion/zone.js that referenced this issue Jun 9, 2017
@hmdhk
Copy link
Contributor Author

hmdhk commented Jun 9, 2017

@JiaLiPassion ,

It seems to me logging zone.name and task are only relevant if the user knows about zone.js library (why I thought it's useful for debugging).

I think in this case it should behave like the native Promise.reject and not to log zone.name and task

zone.js:
image

native:
image

@JiaLiPassion
Copy link
Collaborator

@jahtalab , yes, I understand your meaning, I am not sure which is the better solution, output zone.name and task information will help to debug why unhandled promise rejection exists.

there are way to not output the console.log or even output your own format.

  • this will disable the output.
// after load zone.js
Zone[Zone.__symbol__['ignoreConsoleErrorUncaughtError']] = true;
  • add unhandledPromiseRejectionHandler in window, or handleUnhandledPromiseRejection in node to handle unhandled promise error

  • load your own patch

Zone.__load_patch('ownUncaughtPromiseRejection', (global, Zone, api) => {
  api.onUnhandledError = (e) => {
    // format the error as you will
  }
});

so because we have several ways to let app define how to handle unhandle promise rejection, so I think by default print out zone.name and task is acceptable.

And I am not sure about the native promise behavior, now ZoneAwarePromise pass PromiseA+ test, so Promise A+ spec doesnot seem to define what to do when unhandle promise rejection occurs.

hmdhk added a commit to hmdhk/zone.js that referenced this issue Jun 14, 2017
@Cyral
Copy link

Cyral commented Nov 27, 2017

@JiaLiPassion

The code to disable it should be: (At least with Angular 5, it may have been correct before)

Zone[Zone.__symbol__('ignoreConsoleErrorUncaughtError')] = true;

@JiaLiPassion
Copy link
Collaborator

@Cyral , thank you, my typo

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants