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

feat(longStackTraceSpec): handled promise rejection can also render longstacktrace #631

Merged
merged 1 commit into from
Mar 10, 2017

Conversation

JiaLiPassion
Copy link
Collaborator

@JiaLiPassion JiaLiPassion commented Feb 11, 2017

  1. fix typo in longstacktrace
  2. zone.js have longStackTraceSpec, it is very powerful, but it can't render handled promise rejection.
const longStackTraceZoneSpec = Zone['longStackTraceZoneSpec'];

Zone.current.fork(longStackTraceZoneSpec).run(() => {
  Promise p = new Promise((resolve, reject) => {
    setTimeout(() => {
      reject(new Error('Promise error'));
    }, 10);
  });

  p.catch(error => console.log(error.stack);}
});

In this case, the error p.catch got is not a longStackTrace, because it is a handled promise rejection so it will not call longStackTraceSpec's onHandleError callback.

in this PR, I add a helper method in LongStackTraceZoneSpec, so if user want to render longStackTrace in promise.then/catch, user can call the method.

const longStackTraceZoneSpec = Zone['longStackTraceZoneSpec'];

Zone.current.fork(longStackTraceZoneSpec).run(() => {
  Promise p = new Promise((resolve, reject) => {
    setTimeout(() => {
      reject(new Error('Promise error'));
    }, 10);
  });

  p.catch(error => console.log(longStackTraceZoneSpec.getLongStackTrace(error));}
});

Here, user should call longStackTraceZoneSpec.getLongStackTrace(error) themselves, error.stack
is still the original stack. the reason that not set stack automatically here is because I don't want to break the logic of "handled promise rejection".

@@ -6,6 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/

import {zoneSymbol} from '../common/utils';
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't do imports like that, because it would pull in the whole utils file. This file is meant to be stand alone and so no external imports are allowed.

Just use Zone.__symbol__

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, you are right, I will modify it.

By the way, I just found the same problem exists in my previous PR, for web-apis-mediaquery patch, and web-apis-notification patch, those patches are also be separated, and import function from utils. I will also modify those files to make sure the standalone files not import from utils.

@JiaLiPassion
Copy link
Collaborator Author

@mhevery , I have modified it , please review.

});
promise.catch(function(error) {
expect(longStackTraceZoneSpec.getLongStackTrace(error).split('Elapsed: ').length)
.toBe(4);
Copy link
Contributor

Choose a reason for hiding this comment

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

What exactly is this asserting? Could we make a test which is more explicit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here the asserting means with the longStackTraceZoneSpec.getLongStackTrace method, error which was catch in handing promise rejection can have long stack trace, so here I just assert the stack trace contains Elapsed: string.

@@ -91,6 +92,25 @@ describe('longStackTraceZone', function() {
}, 0);
});
});

it('should produce long stack traces when has handled error in promise', function(done) {
Copy link
Contributor

Choose a reason for hiding this comment

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

...when handling errors in promises

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

modified

@JiaLiPassion
Copy link
Collaborator Author

@mhevery , I have updated the comment, please review whether it is ok. thank you.

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

1 similar comment
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@googlebot googlebot added cla: no and removed cla: yes labels Mar 7, 2017
@googlebot
Copy link

CLAs look good, thanks!

@googlebot
Copy link

CLAs look good, thanks!

@mhevery mhevery merged commit a4c6525 into angular:master Mar 10, 2017
@JiaLiPassion JiaLiPassion deleted the longstack branch March 17, 2017 03:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants