Skip to content

Commit bc09c31

Browse files
Abdulrahman AlmkheibarAbdulrahman Almkheibar
Abdulrahman Almkheibar
authored and
Abdulrahman Almkheibar
committed
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 5f76bc6 commit bc09c31

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

src/minErr.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88

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

1314
/**
@@ -30,12 +31,17 @@ 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+
* * `isUrlParameters` **{Boolean}** - Specifies wether the generated url error will contain the paramters or not.
35+
* Default: true
3336
*/
3437
function errorHandlingConfig(config) {
3538
if (isObject(config)) {
3639
if (isDefined(config.objectMaxDepth)) {
3740
minErrConfig.objectMaxDepth = isValidObjectMaxDepth(config.objectMaxDepth) ? config.objectMaxDepth : NaN;
3841
}
42+
if (isDefined(config.isUrlParameters) && isBoolean(config.isUrlParameters)) {
43+
minErrConfig.isUrlParameters = config.isUrlParameters;
44+
}
3945
} else {
4046
return minErrConfig;
4147
}
@@ -50,6 +56,7 @@ function isValidObjectMaxDepth(maxDepth) {
5056
return isNumber(maxDepth) && maxDepth > 0;
5157
}
5258

59+
5360
/**
5461
* @description
5562
*
@@ -103,9 +110,10 @@ function minErr(module, ErrorConstructor) {
103110

104111
message += '\nhttp://errors.angularjs.org/"NG_VERSION_FULL"/' +
105112
(module ? module + '/' : '') + code;
106-
107-
for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') {
108-
message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]);
113+
if (minErrConfig.isUrlParameters) {
114+
for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') {
115+
message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]);
116+
}
109117
}
110118

111119
return new ErrorConstructor(message);

test/minErrSpec.js

+45-16
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,53 @@
22

33
describe('errors', function() {
44
var originalObjectMaxDepthInErrorMessage = minErrConfig.objectMaxDepth;
5-
5+
var originalIsUrlParameters = minErrConfig.isUrlParameters;
66
afterEach(function() {
77
minErrConfig.objectMaxDepth = originalObjectMaxDepthInErrorMessage;
8+
minErrConfig.isUrlParameters = originalIsUrlParameters;
89
});
910

1011
describe('errorHandlingConfig', function() {
11-
it('should get default objectMaxDepth', function() {
12-
expect(errorHandlingConfig().objectMaxDepth).toBe(5);
12+
describe('objectMaxDepth',function() {
13+
it('should get default objectMaxDepth', function() {
14+
expect(errorHandlingConfig().objectMaxDepth).toBe(5);
15+
});
16+
17+
it('should set objectMaxDepth', function() {
18+
errorHandlingConfig({objectMaxDepth: 3});
19+
expect(errorHandlingConfig().objectMaxDepth).toBe(3);
20+
});
21+
22+
it('should not change objectMaxDepth when undefined is supplied', function() {
23+
errorHandlingConfig({objectMaxDepth: undefined});
24+
expect(errorHandlingConfig().objectMaxDepth).toBe(originalObjectMaxDepthInErrorMessage);
25+
});
26+
27+
they('should set objectMaxDepth to NaN when $prop is supplied',
28+
[NaN, null, true, false, -1, 0], function(maxDepth) {
29+
errorHandlingConfig({objectMaxDepth: maxDepth});
30+
expect(errorHandlingConfig().objectMaxDepth).toBeNaN();
31+
}
32+
);
1333
});
1434

15-
it('should set objectMaxDepth', function() {
16-
errorHandlingConfig({objectMaxDepth: 3});
17-
expect(errorHandlingConfig().objectMaxDepth).toBe(3);
18-
});
1935

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

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-
);
3152
});
3253

3354
describe('minErr', function() {
@@ -164,5 +185,13 @@ describe('errors', function() {
164185
expect(testError('acode', 'aproblem', 'a', 'b', 'value with space').message)
165186
.toMatch(/^[\s\S]*\?p0=a&p1=b&p2=value%20with%20space$/);
166187
});
188+
189+
it('should not generate URL query parameters when isUrlParameters is false', function() {
190+
191+
errorHandlingConfig({isUrlParameters:false});
192+
expect(testError('acode', 'aproblem', 'a', 'b', 'c').message)
193+
.not.toContain('?p0=a&p1=b&p2=c');
194+
});
195+
167196
});
168197
});

0 commit comments

Comments
 (0)