Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/common/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
* found in the LICENSE file at https://angular.io/license
*/
Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
function readableObjectToString(obj: any) {
if (obj && obj.toString === Object.prototype.toString) {
const className = obj.constructor && obj.constructor.name;
return (className ? className : '') + ': ' + JSON.stringify(obj);
}

return obj ? obj.toString() : Object.prototype.toString.call(obj);
}

interface UncaughtPromiseError extends Error {
zone: AmbientZone;
task: Task;
Expand Down Expand Up @@ -164,8 +173,9 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
if (queue.length == 0 && state == REJECTED) {
(promise as any)[symbolState] = REJECTED_NO_CATCH;
try {
// try to print more readable error log
throw new Error(
'Uncaught (in promise): ' + value +
'Uncaught (in promise): ' + readableObjectToString(value) +
(value && value.stack ? '\n' + value.stack : ''));
} catch (err) {
const error: UncaughtPromiseError = err;
Expand Down
37 changes: 37 additions & 0 deletions test/common/Promise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ function flushMicrotasks() {
Zone.current.get('flush')();
}

class TestRejection {
prop1: string;
prop2: string;
}

describe(
'Promise', ifEnvSupports('Promise', function() {
if (!global.Promise) return;
Expand Down Expand Up @@ -344,6 +349,38 @@ describe(
done();
});
});

it('should print readable information when throw a not error object', (done) => {
let promiseError: Error = null;
let zone: Zone = null;
let task: Task = null;
let rejectObj: TestRejection;
queueZone
.fork({
name: 'promise-error',
onHandleError: (delegate: ZoneDelegate, current: Zone, target: Zone, error: any):
boolean => {
promiseError = error;
delegate.handleError(target, error);
return false;
}
})
.run(() => {
zone = Zone.current;
task = Zone.currentTask;
rejectObj = new TestRejection();
rejectObj.prop1 = 'value1';
rejectObj.prop2 = 'value2';
Promise.reject(rejectObj);
expect(promiseError).toBe(null);
});
setTimeout((): void => null);
setTimeout(() => {
expect(promiseError.message)
.toMatch(/Uncaught \(in promise\):.*: {"prop1":"value1","prop2":"value2"}/);
done();
});
});
});

describe('Promise.race', () => {
Expand Down