Skip to content

Commit 7d4f90e

Browse files
committed
Merge pull request #321 from cujojs/safe-json-stringify-unhandled-rejections
Better error messages on unhanlded rejections, guard JSON.stringify
2 parents c642cea + bee0cee commit 7d4f90e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/decorators/unhandledRejection.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,20 @@ define(function(require) {
6666
} else {
6767
s = String(e);
6868
if(s === '[object Object]' && typeof JSON !== 'undefined') {
69-
s = JSON.stringify(e);
69+
s = tryStringify(e, s);
7070
}
7171
}
7272

7373
return e instanceof Error ? s : s + ' (WARNING: non-Error used)';
74+
}
7475

76+
function tryStringify(e, defaultValue) {
77+
try {
78+
return JSON.stringify(e);
79+
} catch(e) {
80+
// Ignore. Cannot JSON.stringify e, stick with String(e)
81+
return defaultValue;
82+
}
7583
}
7684

7785
function noop() {}

test/unhandledRejection-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var buster = typeof window !== 'undefined' ? window.buster : require('buster');
2+
var unhandledRejection = require('../lib/decorators/unhandledRejection');
3+
4+
buster.testCase('unhandledRejection', {
5+
6+
'should not fail if JSON.stringify throws': function(done) {
7+
var fixture = unhandledRejection({}, function(f) {
8+
setTimeout(function() {
9+
buster.refute.exception(f);
10+
done();
11+
});
12+
});
13+
var circular = {};
14+
circular.self = circular;
15+
16+
fixture.onPotentiallyUnhandledRejection({ handled: false, value: circular });
17+
}
18+
19+
});

0 commit comments

Comments
 (0)