Skip to content

Commit 3d98715

Browse files
committed
feat(minErr): set max depth for angular error JSON stringify
closes angular#15402
1 parent 6e91f9c commit 3d98715

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

Diff for: src/Angular.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -815,9 +815,11 @@ function arrayRemove(array, value) {
815815
</file>
816816
</example>
817817
*/
818-
function copy(source, destination) {
818+
function copy(source, destination, maxDepth) {
819819
var stackSource = [];
820820
var stackDest = [];
821+
var currentDepth = 0;
822+
maxDepth = (maxDepth && isNumber(maxDepth) && maxDepth > 0) ? maxDepth : -1;
821823

822824
if (destination) {
823825
if (isTypedArray(destination) || isArrayBuffer(destination)) {
@@ -840,43 +842,48 @@ function copy(source, destination) {
840842

841843
stackSource.push(source);
842844
stackDest.push(destination);
843-
return copyRecurse(source, destination);
845+
return copyRecurse(source, destination, currentDepth);
844846
}
845847

846-
return copyElement(source);
848+
return copyElement(source, currentDepth);
849+
850+
function copyRecurse(source, destination, currentDepth) {
851+
currentDepth++;
852+
if (maxDepth !== -1 && currentDepth > maxDepth) {
853+
return typeof source;
854+
}
847855

848-
function copyRecurse(source, destination) {
849856
var h = destination.$$hashKey;
850857
var key;
851858
if (isArray(source)) {
852859
for (var i = 0, ii = source.length; i < ii; i++) {
853-
destination.push(copyElement(source[i]));
860+
destination.push(copyElement(source[i], currentDepth));
854861
}
855862
} else if (isBlankObject(source)) {
856863
// createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
857864
for (key in source) {
858-
destination[key] = copyElement(source[key]);
865+
destination[key] = copyElement(source[key], currentDepth);
859866
}
860867
} else if (source && typeof source.hasOwnProperty === 'function') {
861868
// Slow path, which must rely on hasOwnProperty
862869
for (key in source) {
863870
if (source.hasOwnProperty(key)) {
864-
destination[key] = copyElement(source[key]);
871+
destination[key] = copyElement(source[key], currentDepth);
865872
}
866873
}
867874
} else {
868875
// Slowest path --- hasOwnProperty can't be called as a method
869876
for (key in source) {
870877
if (hasOwnProperty.call(source, key)) {
871-
destination[key] = copyElement(source[key]);
878+
destination[key] = copyElement(source[key], currentDepth);
872879
}
873880
}
874881
}
875882
setHashKey(destination, h);
876883
return destination;
877884
}
878885

879-
function copyElement(source) {
886+
function copyElement(source, currentDepth) {
880887
// Simple values
881888
if (!isObject(source)) {
882889
return source;
@@ -905,7 +912,7 @@ function copy(source, destination) {
905912
stackDest.push(destination);
906913

907914
return needsRecurse
908-
? copyRecurse(source, destination)
915+
? copyRecurse(source, destination, currentDepth)
909916
: destination;
910917
}
911918

Diff for: src/minErr.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function minErr(module, ErrorConstructor) {
3434
ErrorConstructor = ErrorConstructor || Error;
3535
return function() {
3636
var SKIP_INDEXES = 2;
37+
var MAX_DEPTH = 1;
3738

3839
var templateArgs = arguments,
3940
code = templateArgs[0],
@@ -46,7 +47,7 @@ function minErr(module, ErrorConstructor) {
4647
shiftedIndex = index + SKIP_INDEXES;
4748

4849
if (shiftedIndex < templateArgs.length) {
49-
return toDebugString(templateArgs[shiftedIndex]);
50+
return toDebugString(templateArgs[shiftedIndex], MAX_DEPTH);
5051
}
5152

5253
return match;
@@ -57,7 +58,7 @@ function minErr(module, ErrorConstructor) {
5758

5859
for (i = SKIP_INDEXES, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') {
5960
message += paramPrefix + 'p' + (i - SKIP_INDEXES) + '=' +
60-
encodeURIComponent(toDebugString(templateArgs[i]));
61+
encodeURIComponent(toDebugString(templateArgs[i]), MAX_DEPTH);
6162
}
6263

6364
return new ErrorConstructor(message);

Diff for: src/stringify.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
/* global toDebugString: true */
44

5-
function serializeObject(obj) {
5+
function serializeObject(obj, maxDepth) {
66
var seen = [];
7+
var tempObj = {};
8+
if (maxDepth && isNumber(maxDepth) && maxDepth > 0) {
9+
copy(obj, tempObj, maxDepth)
10+
} else {
11+
tempObj = obj;
12+
}
713

8-
return JSON.stringify(obj, function(key, val) {
14+
return JSON.stringify(tempObj, function(key, val) {
915
val = toJsonReplacer(key, val);
1016
if (isObject(val)) {
1117

@@ -17,13 +23,13 @@ function serializeObject(obj) {
1723
});
1824
}
1925

20-
function toDebugString(obj) {
26+
function toDebugString(obj, maxDepth) {
2127
if (typeof obj === 'function') {
2228
return obj.toString().replace(/ \{[\s\S]*$/, '');
2329
} else if (isUndefined(obj)) {
2430
return 'undefined';
2531
} else if (typeof obj !== 'string') {
26-
return serializeObject(obj);
32+
return serializeObject(obj, maxDepth);
2733
}
2834
return obj;
2935
}

0 commit comments

Comments
 (0)