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

Commit 4b2de4b

Browse files
committed
feat(minErr): strip error url from error parameters
Related #14744
1 parent 786cb30 commit 4b2de4b

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/minErr.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ function isValidObjectMaxDepth(maxDepth) {
8282

8383
function minErr(module, ErrorConstructor) {
8484
ErrorConstructor = ErrorConstructor || Error;
85+
86+
var url = 'https://errors.angularjs.org/"NG_VERSION_FULL"/';
87+
var regex = url.replace('.', '\\.') + '[\\s\\S]*';
88+
var errRegExp = new RegExp(regex, 'g');
89+
8590
return function() {
8691
var code = arguments[0],
8792
template = arguments[1],
@@ -91,18 +96,22 @@ function minErr(module, ErrorConstructor) {
9196
}),
9297
paramPrefix, i;
9398

99+
// A minErr message has two parts: the message itself and the url that contains the
100+
// encoded message.
101+
// The message's parameters can contain other error messages which also include error urls.
102+
// To prevent the messages from getting too long, we strip the error urls from the parameters.
103+
94104
message += template.replace(/\{\d+\}/g, function(match) {
95105
var index = +match.slice(1, -1);
96106

97107
if (index < templateArgs.length) {
98-
return templateArgs[index];
108+
return templateArgs[index].replace(errRegExp, '');
99109
}
100110

101111
return match;
102112
});
103113

104-
message += '\nhttps://errors.angularjs.org/"NG_VERSION_FULL"/' +
105-
(module ? module + '/' : '') + code;
114+
message += '\n' + url + (module ? module + '/' : '') + code;
106115

107116
for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') {
108117
message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]);

test/minErrSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,18 @@ describe('errors', function() {
164164
expect(testError('acode', 'aproblem', 'a', 'b', 'value with space').message)
165165
.toMatch(/^[\s\S]*\?p0=a&p1=b&p2=value%20with%20space$/);
166166
});
167+
168+
169+
it('should strip error reference urls from the error message parameters', function() {
170+
var firstError = testError('firstcode', 'longer string and so on');
171+
172+
var error = testError('secondcode', 'description {0}, and {1}', 'a', firstError.message);
173+
174+
expect(error.message).toBe('[test:secondcode] description a, and [test:firstcode] longer ' +
175+
'string and so on\n\nhttps://errors.angularjs.org/"NG_VERSION_FULL"/test/' +
176+
'secondcode?p0=a&p1=%5Btest%3Afirstcode%5D%20longer%20string%20and%20so%20on%0Ahttps' +
177+
'%3A%2F%2Ferrors.angularjs.org%2F%22NG_VERSION_FULL%22%2Ftest%2Ffirstcode');
178+
});
179+
167180
});
168181
});

0 commit comments

Comments
 (0)