feat(minErr): strip error url from error parameters

Related https://github.com/angular/angular.js/issues/14744
This commit is contained in:
Martin Staffa
2018-03-16 12:21:52 +01:00
committed by Martin Staffa
parent 786cb30c61
commit 4b2de4bcbf
2 changed files with 25 additions and 3 deletions
+12 -3
View File
@@ -82,6 +82,11 @@ function isValidObjectMaxDepth(maxDepth) {
function minErr(module, ErrorConstructor) {
ErrorConstructor = ErrorConstructor || Error;
var url = 'https://errors.angularjs.org/"NG_VERSION_FULL"/';
var regex = url.replace('.', '\\.') + '[\\s\\S]*';
var errRegExp = new RegExp(regex, 'g');
return function() {
var code = arguments[0],
template = arguments[1],
@@ -91,18 +96,22 @@ function minErr(module, ErrorConstructor) {
}),
paramPrefix, i;
// A minErr message has two parts: the message itself and the url that contains the
// encoded message.
// The message's parameters can contain other error messages which also include error urls.
// To prevent the messages from getting too long, we strip the error urls from the parameters.
message += template.replace(/\{\d+\}/g, function(match) {
var index = +match.slice(1, -1);
if (index < templateArgs.length) {
return templateArgs[index];
return templateArgs[index].replace(errRegExp, '');
}
return match;
});
message += '\nhttps://errors.angularjs.org/"NG_VERSION_FULL"/' +
(module ? module + '/' : '') + code;
message += '\n' + url + (module ? module + '/' : '') + code;
for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') {
message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]);
+13
View File
@@ -164,5 +164,18 @@ describe('errors', function() {
expect(testError('acode', 'aproblem', 'a', 'b', 'value with space').message)
.toMatch(/^[\s\S]*\?p0=a&p1=b&p2=value%20with%20space$/);
});
it('should strip error reference urls from the error message parameters', function() {
var firstError = testError('firstcode', 'longer string and so on');
var error = testError('secondcode', 'description {0}, and {1}', 'a', firstError.message);
expect(error.message).toBe('[test:secondcode] description a, and [test:firstcode] longer ' +
'string and so on\n\nhttps://errors.angularjs.org/"NG_VERSION_FULL"/test/' +
'secondcode?p0=a&p1=%5Btest%3Afirstcode%5D%20longer%20string%20and%20so%20on%0Ahttps' +
'%3A%2F%2Ferrors.angularjs.org%2F%22NG_VERSION_FULL%22%2Ftest%2Ffirstcode');
});
});
});