Skip to content

Commit fea0dd7

Browse files
Abdulrahman AlmkheibarNarretz
Abdulrahman Almkheibar
authored andcommitted
feat(errorHandlingConfig): add an option so that the parameters of URL error is truncated
In Angular.js the error in loading a module will lead to a failur in loading the dependent modules. In turn, this leads to a chain of errors in loading those modules. For some reason, angularjs is copying the error stack and passes it to the next error. This stack is encoded in URL error,so we end up sometimes with so long Error URL when we have may dependent modules. This option disables encoding the parameters in URL error: angular.errorHandlingConfig({isUrlParameter:false}); closes angular#14744
1 parent 0ed3643 commit fea0dd7

File tree

2 files changed

+65
-20
lines changed

2 files changed

+65
-20
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

+49-17
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,58 @@
11
'use strict';
22

3-
describe('errors', function() {
3+
fdescribe('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)