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:
Peter Bacon Darwin
2014-11-17 07:07:43 +00:00
parent a9352c19ce
commit cf43ccdf9b
7 changed files with 58 additions and 20 deletions

View File

@@ -18,6 +18,7 @@
"angularModule": false,
"nodeName_": false,
"uid": false,
"toDebugString": false,
"lowercase": false,
"uppercase": false,

View File

@@ -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
View 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>>"]');
});
});