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

Commit 6136d59

Browse files
committed
feat(errorHandlingConfig): make the length of URL refrence in error messages configurable
Closes #14744 Closes #15707
1 parent 71f437c commit 6136d59

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

src/Angular.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ var VALIDITY_STATE_PROPERTY = 'validity';
129129
var hasOwnProperty = Object.prototype.hasOwnProperty;
130130

131131
var minErrConfig = {
132-
objectMaxDepth: 5
132+
objectMaxDepth: 5,
133+
urlMaxLength: 2000
133134
};
134135

135136
/**
@@ -143,6 +144,7 @@ var minErrConfig = {
143144
* current configuration if used as a getter. The following options are supported:
144145
*
145146
* - **objectMaxDepth**: The maximum depth to which objects are traversed when stringified for error messages.
147+
* - **urlMaxLength**: The maximum length of the URL reference in the error messages.
146148
*
147149
* Omitted or undefined options will leave the corresponding configuration values unchanged.
148150
*
@@ -152,12 +154,19 @@ var minErrConfig = {
152154
* * `objectMaxDepth` **{Number}** - The max depth for stringifying objects. Setting to a
153155
* non-positive or non-numeric value, removes the max depth limit.
154156
* Default: 5
157+
*
158+
* * `urlMaxLength` **{Number}** - The max length of the URL reference in the error messages, it can be any number more than or equal 200. Setting to a
159+
* non-positive or non-numeric value, removes the url max length limit.
160+
* Default: 2000
155161
*/
156162
function errorHandlingConfig(config) {
157163
if (isObject(config)) {
158164
if (isDefined(config.objectMaxDepth)) {
159165
minErrConfig.objectMaxDepth = isValidObjectMaxDepth(config.objectMaxDepth) ? config.objectMaxDepth : NaN;
160166
}
167+
if(isDefined(config.urlMaxLength)) {
168+
minErrConfig.urlMaxLength = (isNumber(config.urlMaxLength) && config.urlMaxLength >= 200) ? config.urlMaxLength : NaN;
169+
}
161170
} else {
162171
return minErrConfig;
163172
}

src/minErr.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,21 @@ function minErr(module, ErrorConstructor) {
5151
return match;
5252
});
5353

54-
message += '\nhttp://errors.angularjs.org/"NG_VERSION_FULL"/' +
54+
var URL_MAX_LENGTH_END = '...';
55+
var url = 'http://errors.angularjs.org/"NG_VERSION_FULL"/' +
5556
(module ? module + '/' : '') + code;
5657

5758
for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') {
58-
message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]);
59+
url += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]);
60+
61+
if(url.length > minErrConfig.urlMaxLength) {
62+
url = url.slice(0, minErrConfig.urlMaxLength - URL_MAX_LENGTH_END.length);
63+
url += URL_MAX_LENGTH_END;
64+
break;
65+
}
5966
}
6067

68+
message += '\n' + url;
6169
return new ErrorConstructor(message);
6270
};
6371
}

test/minErrSpec.js

+46
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,29 @@ describe('minErr', function() {
1010
testError = minErr('test');
1111

1212
var originalObjectMaxDepthInErrorMessage = minErrConfig.objectMaxDepth;
13+
var originalUrlMaxLengthInErrorMessage = minErrConfig.urlMaxLength;
1314
afterEach(function() {
1415
minErrConfig.objectMaxDepth = originalObjectMaxDepthInErrorMessage;
16+
minErrConfig.urlMaxLength = originalUrlMaxLengthInErrorMessage;
1517
});
1618

19+
function extractUrlFromErrorMessage(message) {
20+
var match = message.match(/http[\s\S]*\?p0=/);
21+
var urlStartAt = message.indexOf(match[0]);
22+
if(urlStartAt < 0) {
23+
throw new Error('Could not find url');
24+
}
25+
return message.slice(urlStartAt);
26+
}
27+
28+
function createDummyString(size) {
29+
var str = '';
30+
for(var i = 0; i < size; i++) {
31+
str += 'a'
32+
}
33+
return str;
34+
}
35+
1736
it('should return an Error factory', function() {
1837
var myError = testError('test', 'Oops');
1938
expect(myError instanceof Error).toBe(true);
@@ -143,4 +162,31 @@ describe('minErr', function() {
143162
expect(testError('acode', 'aproblem', 'a', 'b', 'value with space').message)
144163
.toMatch(/^[\s\S]*\?p0=a&p1=b&p2=value%20with%20space$/);
145164
});
165+
166+
it('should slice error reference URL in the message if it exceeds url max length', function() {
167+
debugger;
168+
var a = createDummyString(2000);
169+
170+
var myError = testError('26', 'a when default=2000 is {0}', a);
171+
var url = extractUrlFromErrorMessage(myError.message);
172+
expect(url.length).toBe(2000);
173+
expect(errorHandlingConfig().urlMaxLength).toBe(2000);
174+
175+
errorHandlingConfig({urlMaxLength: 500});
176+
myError = testError('26', 'a when urlMaxLength=500 is {0}', a);
177+
url = extractUrlFromErrorMessage(myError.message);
178+
expect(url.length).toBe(500);
179+
expect(errorHandlingConfig().urlMaxLength).toBe(500);
180+
});
181+
182+
they('should ignore url max length when urlMaxLength = $prop',
183+
[NaN, null, true, false, -1, 0], function(maxLength) {
184+
errorHandlingConfig({urlMaxLength: maxLength});
185+
var a = createDummyString(2100);
186+
var myError = testError('26', 'a is {0}', a);
187+
var url = extractUrlFromErrorMessage(myError.message);
188+
expect(url.length).toBeGreaterThan(2100);
189+
expect(errorHandlingConfig().urlMaxLength).toBeNaN();
190+
}
191+
);
146192
});

0 commit comments

Comments
 (0)