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

Commit 3d6c45d

Browse files
committed
feat(errorHandlingConfig): add option to exclude error params from url
Specific errors, such as those during nested module loading, can create very long error urls because the error message includes the error stack. These urls create visual clutter in the browser console, are often not clickable, and may be rejected by the docs page because they are simply too long. We've already made improvements to the error display in #16283, which excludes the error url from error parameters, which results in cleaner error messages. Further, modern browsers restrict console message length intelligently. This option can still be useful for older browsers like Internet Explorer, or in general to reduce visual clutter in the console. Closes #14744 Closes #15707 Closes #16283 Closes #16299 Closes #16591
1 parent bf841d3 commit 3d6c45d

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

src/minErr.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88

99
var minErrConfig = {
10-
objectMaxDepth: 5
10+
objectMaxDepth: 5,
11+
urlErrorParamsEnabled: true
1112
};
1213

1314
/**
@@ -30,12 +31,21 @@ var minErrConfig = {
3031
* * `objectMaxDepth` **{Number}** - The max depth for stringifying objects. Setting to a
3132
* non-positive or non-numeric value, removes the max depth limit.
3233
* Default: 5
34+
*
35+
* * `urlErrorParamsEnabled` **{Boolean}** - Specifies wether the generated error url will
36+
* contain the parameters of the thrown error. Disabling the parameters can be useful if the
37+
* generated error url is very long.
38+
*
39+
* Default: true. When used without argument, it returns the current value.
3340
*/
3441
function errorHandlingConfig(config) {
3542
if (isObject(config)) {
3643
if (isDefined(config.objectMaxDepth)) {
3744
minErrConfig.objectMaxDepth = isValidObjectMaxDepth(config.objectMaxDepth) ? config.objectMaxDepth : NaN;
3845
}
46+
if (isDefined(config.urlErrorParamsEnabled) && isBoolean(config.urlErrorParamsEnabled)) {
47+
minErrConfig.urlErrorParamsEnabled = config.urlErrorParamsEnabled;
48+
}
3949
} else {
4050
return minErrConfig;
4151
}
@@ -50,6 +60,7 @@ function isValidObjectMaxDepth(maxDepth) {
5060
return isNumber(maxDepth) && maxDepth > 0;
5161
}
5262

63+
5364
/**
5465
* @description
5566
*
@@ -113,8 +124,10 @@ function minErr(module, ErrorConstructor) {
113124

114125
message += '\n' + url + (module ? module + '/' : '') + code;
115126

116-
for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') {
117-
message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]);
127+
if (minErrConfig.urlErrorParamsEnabled) {
128+
for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') {
129+
message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]);
130+
}
118131
}
119132

120133
return new ErrorConstructor(message);

test/minErrSpec.js

+48-16
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,57 @@
22

33
describe('errors', function() {
44
var originalObjectMaxDepthInErrorMessage = minErrConfig.objectMaxDepth;
5+
var originalUrlErrorParamsEnabled = minErrConfig.urlErrorParamsEnabled;
56

67
afterEach(function() {
78
minErrConfig.objectMaxDepth = originalObjectMaxDepthInErrorMessage;
9+
minErrConfig.urlErrorParamsEnabled = originalUrlErrorParamsEnabled;
810
});
911

1012
describe('errorHandlingConfig', function() {
11-
it('should get default objectMaxDepth', function() {
12-
expect(errorHandlingConfig().objectMaxDepth).toBe(5);
13-
});
13+
describe('objectMaxDepth',function() {
14+
it('should get default objectMaxDepth', function() {
15+
expect(errorHandlingConfig().objectMaxDepth).toBe(5);
16+
});
17+
18+
it('should set objectMaxDepth', function() {
19+
errorHandlingConfig({objectMaxDepth: 3});
20+
expect(errorHandlingConfig().objectMaxDepth).toBe(3);
21+
});
1422

15-
it('should set objectMaxDepth', function() {
16-
errorHandlingConfig({objectMaxDepth: 3});
17-
expect(errorHandlingConfig().objectMaxDepth).toBe(3);
23+
it('should not change objectMaxDepth when undefined is supplied', function() {
24+
errorHandlingConfig({objectMaxDepth: undefined});
25+
expect(errorHandlingConfig().objectMaxDepth).toBe(originalObjectMaxDepthInErrorMessage);
26+
});
27+
28+
they('should set objectMaxDepth to NaN when $prop is supplied',
29+
[NaN, null, true, false, -1, 0], function(maxDepth) {
30+
errorHandlingConfig({objectMaxDepth: maxDepth});
31+
expect(errorHandlingConfig().objectMaxDepth).toBeNaN();
32+
}
33+
);
1834
});
1935

20-
it('should not change objectMaxDepth when undefined is supplied', function() {
21-
errorHandlingConfig({objectMaxDepth: undefined});
22-
expect(errorHandlingConfig().objectMaxDepth).toBe(originalObjectMaxDepthInErrorMessage);
36+
37+
describe('urlErrorParamsEnabled',function() {
38+
39+
it('should get default urlErrorParamsEnabled', function() {
40+
expect(errorHandlingConfig().urlErrorParamsEnabled).toBe(true);
41+
});
42+
43+
it('should set urlErrorParamsEnabled', function() {
44+
errorHandlingConfig({urlErrorParamsEnabled: false});
45+
expect(errorHandlingConfig().urlErrorParamsEnabled).toBe(false);
46+
errorHandlingConfig({urlErrorParamsEnabled: true});
47+
expect(errorHandlingConfig().urlErrorParamsEnabled).toBe(true);
48+
});
49+
50+
it('should not change its value when non-boolean is supplied', function() {
51+
errorHandlingConfig({urlErrorParamsEnabled: 123});
52+
expect(errorHandlingConfig().urlErrorParamsEnabled).toBe(originalUrlErrorParamsEnabled);
53+
});
2354
});
2455

25-
they('should set objectMaxDepth to NaN when $prop is supplied',
26-
[NaN, null, true, false, -1, 0], function(maxDepth) {
27-
errorHandlingConfig({objectMaxDepth: maxDepth});
28-
expect(errorHandlingConfig().objectMaxDepth).toBeNaN();
29-
}
30-
);
3156
});
3257

3358
describe('minErr', function() {
@@ -165,7 +190,6 @@ describe('errors', function() {
165190
.toMatch(/^[\s\S]*\?p0=a&p1=b&p2=value%20with%20space$/);
166191
});
167192

168-
169193
it('should strip error reference urls from the error message parameters', function() {
170194
var firstError = testError('firstcode', 'longer string and so on');
171195

@@ -177,5 +201,13 @@ describe('errors', function() {
177201
'%3A%2F%2Ferrors.angularjs.org%2F%22NG_VERSION_FULL%22%2Ftest%2Ffirstcode');
178202
});
179203

204+
it('should not generate URL query parameters when urlErrorParamsEnabled is false', function() {
205+
206+
errorHandlingConfig({urlErrorParamsEnabled: false});
207+
208+
expect(testError('acode', 'aproblem', 'a', 'b', 'c').message).toBe('[test:acode] aproblem\n' +
209+
'https://errors.angularjs.org/"NG_VERSION_FULL"/test/acode');
210+
});
211+
180212
});
181213
});

0 commit comments

Comments
 (0)