Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(toJson): add ability to suppress exceptions #10065

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 13 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,20 @@ function toJsonReplacer(key, value) {
* @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace.
* @returns {string|undefined} JSON-ified string representing `obj`.
*/
function toJson(obj, pretty) {
function toJson(obj, pretty, options) {
if (typeof obj === 'undefined') return undefined;

if (!options) options = {};

if (options.suppressExceptions) {
try {
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
} catch (e) {
// in case JSON.stringify throws, suppress error and return undefined
return undefined;
}
}

return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
}

Expand Down
2 changes: 1 addition & 1 deletion src/minErr.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function minErr(module, ErrorConstructor) {
} else if (typeof arg === 'undefined') {
return 'undefined';
} else if (typeof arg !== 'string') {
return toJson(arg);
return toJson(arg, false, {suppressExceptions: true});
}
return arg;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
});
throw ngRepeatMinErr('dupes',
"Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}, Duplicate value: {2}",
expression, trackById, toJson(value));
expression, trackById, toJson(value, false, {suppressExceptions: true}));
} else {
// new never before seen block
nextBlockOrder[index] = {id: trackById, scope: undefined, clone: undefined};
Expand Down
5 changes: 3 additions & 2 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ function $RootScopeProvider() {
logMsg = (isFunction(watch.exp))
? 'fn: ' + (watch.exp.name || watch.exp.toString())
: watch.exp;
logMsg += '; newVal: ' + toJson(value) + '; oldVal: ' + toJson(last);
logMsg += '; newVal: ' + toJson(value, false, {suppressExceptions: true}) +
'; oldVal: ' + toJson(last, false, {suppressExceptions: true});
watchLog[logIdx].push(logMsg);
}
} else if (watch === lastDirtyWatch) {
Expand Down Expand Up @@ -810,7 +811,7 @@ function $RootScopeProvider() {
throw $rootScopeMinErr('infdig',
'{0} $digest() iterations reached. Aborting!\n' +
'Watchers fired in the last 5 iterations: {1}',
TTL, toJson(watchLog));
TTL, toJson(watchLog, false, {suppressExceptions: true}));
}

} while (dirty || asyncQueue.length);
Expand Down
27 changes: 27 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,33 @@ describe('angular', function() {
it('should serialize undefined as undefined', function() {
expect(toJson(undefined)).toEqual(undefined);
});

it('should allow JSON.stringify to throw exceptions by default', function() {
// create objects with references to each other (circular reference)
// passing such object to JSON.stringify throws a TypeError
var a = {}, b = {test: a};
a.test = b;

// prepare toJson function with correct arguments for this test
var toJsonSuppressedTest = toJson.bind(undefined, a);

// it should throw a TypeError
expect(toJsonSuppressedTest).toThrow();
});

it('should not throw exceptions if suppressExceptions option is passed', function() {
// create objects with references to each other (circular reference)
// passing such object to JSON.stringify throws a TypeError
var a = {}, b = {test: a};
a.test = b;

// prepare toJson function with correct arguments for this test
var toJsonSuppressedTest = toJson.bind(undefined, a, false, {suppressExceptions: true});

// it shouldn't throw a TypeError and it should return undefined
expect(toJsonSuppressedTest).not.toThrow();
expect(toJsonSuppressedTest()).toBeUndefined();
});
});

describe('isElement', function() {
Expand Down