Skip to content

Commit

Permalink
fix(promise): include stack trace in an unhandlerd promise (angular#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery authored Sep 20, 2016
1 parent 0c3ce2e commit 737f8d8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ const Zone: ZoneType = (function(global: any) {
if (this.data && typeof this.data.handleId !== 'undefined') {
return this.data.handleId;
} else {
return this.toString();
return Object.prototype.toString.call(this);
}
}
}
Expand Down Expand Up @@ -994,7 +994,7 @@ const Zone: ZoneType = (function(global: any) {
if (queue.length == 0 && state == REJECTED) {
promise[symbolState] = REJECTED_NO_CATCH;
try {
throw new Error("Uncaught (in promise): " + value);
throw new Error("Uncaught (in promise): " + value + (value && value.stack ? '\n' + value.stack : ''));
} catch (e) {
const error: UncaughtPromiseError = e;
error.rejection = value;
Expand Down
13 changes: 10 additions & 3 deletions test/common/Promise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ describe('Promise', ifEnvSupports('Promise', function () {
var promiseError: Error = null;
var zone: Zone = null;
var task: Task = null;
var error: Error = null;
queueZone.fork({
name: 'promise-error',
onHandleError: (delegate: ZoneDelegate, current: Zone, target: Zone,
Expand All @@ -244,13 +245,19 @@ describe('Promise', ifEnvSupports('Promise', function () {
}).run(() => {
zone = Zone.current;
task = Zone.currentTask;
Promise.reject('rejectedErrorShouldBeHandled');
error = new Error('rejectedErrorShouldBeHandled');
try {
// throw so that the stack trace is captured
throw error;
} catch (e) {}
Promise.reject(error);
expect(promiseError).toBe(null);
});
setTimeout(() => null);
setTimeout(() => {
expect(promiseError.message).toBe('Uncaught (in promise): rejectedErrorShouldBeHandled');
expect(promiseError['rejection']).toBe('rejectedErrorShouldBeHandled');
expect(promiseError.message).toBe('Uncaught (in promise): ' + error +
(error.stack ? '\n' + error.stack : ''));
expect(promiseError['rejection']).toBe(error);
expect(promiseError['zone']).toBe(zone);
expect(promiseError['task']).toBe(task);
done();
Expand Down
2 changes: 1 addition & 1 deletion test/zone-spec/fake-async-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('FakeAsyncTestZoneSpec', () => {
fakeAsyncTestZone.run(() => {
Promise.resolve(null).then((_) => { throw new Error('async'); });
expect(() => { testZoneSpec.flushMicrotasks(); })
.toThrowError('Uncaught (in promise): Error: async');
.toThrowError(/Uncaught \(in promise\): Error: async/);
});
});

Expand Down

0 comments on commit 737f8d8

Please sign in to comment.