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

Commit

Permalink
feat(minErr): strip error url from error parameters
Browse files Browse the repository at this point in the history
Related #14744
  • Loading branch information
Narretz committed Mar 19, 2018
1 parent 52e4668 commit 980b69d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/minErr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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]);
Expand Down
13 changes: 13 additions & 0 deletions test/minErrSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

});
});

0 comments on commit 980b69d

Please sign in to comment.