mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-28 12:55:48 +08:00
fix(minErr): stringify non-JSON compatible objects in error messages
Fix the JSON stringification to output a more meaningful string when an object cannot be normally converted to a JSON string, such as when the object contains cyclic references that would cause `JSON.stringify()` to throw an error. Closes #10085
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
"angularModule": false,
|
||||
"nodeName_": false,
|
||||
"uid": false,
|
||||
"toDebugString": false,
|
||||
|
||||
"lowercase": false,
|
||||
"uppercase": false,
|
||||
|
||||
@@ -60,6 +60,13 @@ describe('minErr', function() {
|
||||
toMatch(/^\[test:26\] false: false; zero: 0; null: null; undefined: undefined; emptyStr: /);
|
||||
});
|
||||
|
||||
it('should handle arguments that are objects with cyclic references', function() {
|
||||
var a = { b: { } };
|
||||
a.b.a = a;
|
||||
|
||||
var myError = testError('26', 'a is {0}', a);
|
||||
expect(myError.message).toMatch(/a is {"b":{"a":"<<already seen>>"}}/);
|
||||
});
|
||||
|
||||
it('should preserve interpolation markers when fewer arguments than needed are provided', function() {
|
||||
// this way we can easily see if we are passing fewer args than needed
|
||||
|
||||
15
test/stringifySpec.js
Normal file
15
test/stringifySpec.js
Normal file
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
describe('toDebugString', function() {
|
||||
it('should convert its argument to a string', function() {
|
||||
expect(toDebugString('string')).toEqual('string');
|
||||
expect(toDebugString(123)).toEqual('123');
|
||||
expect(toDebugString({a:{b:'c'}})).toEqual('{"a":{"b":"c"}}');
|
||||
expect(toDebugString(function fn() { var a = 10; })).toEqual('function fn()');
|
||||
expect(toDebugString()).toEqual('undefined');
|
||||
var a = { };
|
||||
a.a = a;
|
||||
expect(toDebugString(a)).toEqual('{"a":"<<already seen>>"}');
|
||||
expect(toDebugString([a,a])).toEqual('[{"a":"<<already seen>>"},"<<already seen>>"]');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user