Skip to content

Commit 6773b71

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

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

src/Angular.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -815,9 +815,14 @@ 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+
823+
if (!isNumber(maxDepth)) {
824+
maxDepth = NaN;
825+
}
821826

822827
if (destination) {
823828
if (isTypedArray(destination) || isArrayBuffer(destination)) {
@@ -840,43 +845,47 @@ function copy(source, destination) {
840845

841846
stackSource.push(source);
842847
stackDest.push(destination);
843-
return copyRecurse(source, destination);
848+
return copyRecurse(source, destination, currentDepth);
844849
}
845850

846-
return copyElement(source);
851+
return copyElement(source, currentDepth);
847852

848-
function copyRecurse(source, destination) {
853+
function copyRecurse(source, destination, currentDepth) {
854+
currentDepth++;
855+
if (currentDepth > maxDepth) {
856+
return '...';
857+
}
849858
var h = destination.$$hashKey;
850859
var key;
851860
if (isArray(source)) {
852861
for (var i = 0, ii = source.length; i < ii; i++) {
853-
destination.push(copyElement(source[i]));
862+
destination.push(copyElement(source[i], currentDepth));
854863
}
855864
} else if (isBlankObject(source)) {
856865
// createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
857866
for (key in source) {
858-
destination[key] = copyElement(source[key]);
867+
destination[key] = copyElement(source[key], currentDepth);
859868
}
860869
} else if (source && typeof source.hasOwnProperty === 'function') {
861870
// Slow path, which must rely on hasOwnProperty
862871
for (key in source) {
863872
if (source.hasOwnProperty(key)) {
864-
destination[key] = copyElement(source[key]);
873+
destination[key] = copyElement(source[key], currentDepth);
865874
}
866875
}
867876
} else {
868877
// Slowest path --- hasOwnProperty can't be called as a method
869878
for (key in source) {
870879
if (hasOwnProperty.call(source, key)) {
871-
destination[key] = copyElement(source[key]);
880+
destination[key] = copyElement(source[key], currentDepth);
872881
}
873882
}
874883
}
875884
setHashKey(destination, h);
876885
return destination;
877886
}
878887

879-
function copyElement(source) {
888+
function copyElement(source, currentDepth) {
880889
// Simple values
881890
if (!isObject(source)) {
882891
return source;
@@ -905,7 +914,7 @@ function copy(source, destination) {
905914
stackDest.push(destination);
906915

907916
return needsRecurse
908-
? copyRecurse(source, destination)
917+
? copyRecurse(source, destination, currentDepth)
909918
: destination;
910919
}
911920

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);

src/stringify.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
/* global toDebugString: true */
44

5-
function serializeObject(obj) {
5+
function serializeObject(obj, maxDepth) {
66
var seen = [];
7-
7+
var tempObj = {};
8+
if (maxDepth > 0) {
9+
obj = copy(obj, {}, maxDepth)
10+
}
811
return JSON.stringify(obj, function(key, val) {
912
val = toJsonReplacer(key, val);
1013
if (isObject(val)) {
@@ -17,13 +20,13 @@ function serializeObject(obj) {
1720
});
1821
}
1922

20-
function toDebugString(obj) {
23+
function toDebugString(obj, maxDepth) {
2124
if (typeof obj === 'function') {
2225
return obj.toString().replace(/ \{[\s\S]*$/, '');
2326
} else if (isUndefined(obj)) {
2427
return 'undefined';
2528
} else if (typeof obj !== 'string') {
26-
return serializeObject(obj);
29+
return serializeObject(obj, maxDepth);
2730
}
2831
return obj;
2932
}

0 commit comments

Comments
 (0)