From bc09c31c428d3138bbe1d058df73fb1cc1d479cc Mon Sep 17 00:00:00 2001 From: Abdulrahman Almkheibar Date: Mon, 16 Oct 2017 16:02:42 +0200 Subject: [PATCH] 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 #14744 --- src/minErr.js | 16 +++++++++--- test/minErrSpec.js | 61 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/minErr.js b/src/minErr.js index e20040319222..d636d171984c 100644 --- a/src/minErr.js +++ b/src/minErr.js @@ -7,7 +7,8 @@ */ var minErrConfig = { - objectMaxDepth: 5 + objectMaxDepth: 5, + isUrlParameters:true }; /** @@ -30,12 +31,17 @@ var minErrConfig = { * * `objectMaxDepth` **{Number}** - The max depth for stringifying objects. Setting to a * non-positive or non-numeric value, removes the max depth limit. * Default: 5 + * * `isUrlParameters` **{Boolean}** - Specifies wether the generated url error will contain the paramters or not. + * Default: true */ function errorHandlingConfig(config) { if (isObject(config)) { if (isDefined(config.objectMaxDepth)) { minErrConfig.objectMaxDepth = isValidObjectMaxDepth(config.objectMaxDepth) ? config.objectMaxDepth : NaN; } + if (isDefined(config.isUrlParameters) && isBoolean(config.isUrlParameters)) { + minErrConfig.isUrlParameters = config.isUrlParameters; + } } else { return minErrConfig; } @@ -50,6 +56,7 @@ function isValidObjectMaxDepth(maxDepth) { return isNumber(maxDepth) && maxDepth > 0; } + /** * @description * @@ -103,9 +110,10 @@ function minErr(module, ErrorConstructor) { message += '\nhttp://errors.angularjs.org/"NG_VERSION_FULL"/' + (module ? module + '/' : '') + code; - - for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') { - message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]); + if (minErrConfig.isUrlParameters) { + for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') { + message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]); + } } return new ErrorConstructor(message); diff --git a/test/minErrSpec.js b/test/minErrSpec.js index aae001cba415..201356c5605f 100644 --- a/test/minErrSpec.js +++ b/test/minErrSpec.js @@ -2,32 +2,53 @@ describe('errors', function() { var originalObjectMaxDepthInErrorMessage = minErrConfig.objectMaxDepth; - + var originalIsUrlParameters = minErrConfig.isUrlParameters; afterEach(function() { minErrConfig.objectMaxDepth = originalObjectMaxDepthInErrorMessage; + minErrConfig.isUrlParameters = originalIsUrlParameters; }); describe('errorHandlingConfig', function() { - it('should get default objectMaxDepth', function() { - expect(errorHandlingConfig().objectMaxDepth).toBe(5); + describe('objectMaxDepth',function() { + it('should get default objectMaxDepth', function() { + expect(errorHandlingConfig().objectMaxDepth).toBe(5); + }); + + it('should set objectMaxDepth', function() { + errorHandlingConfig({objectMaxDepth: 3}); + expect(errorHandlingConfig().objectMaxDepth).toBe(3); + }); + + it('should not change objectMaxDepth when undefined is supplied', function() { + errorHandlingConfig({objectMaxDepth: undefined}); + expect(errorHandlingConfig().objectMaxDepth).toBe(originalObjectMaxDepthInErrorMessage); + }); + + they('should set objectMaxDepth to NaN when $prop is supplied', + [NaN, null, true, false, -1, 0], function(maxDepth) { + errorHandlingConfig({objectMaxDepth: maxDepth}); + expect(errorHandlingConfig().objectMaxDepth).toBeNaN(); + } + ); }); - it('should set objectMaxDepth', function() { - errorHandlingConfig({objectMaxDepth: 3}); - expect(errorHandlingConfig().objectMaxDepth).toBe(3); - }); - it('should not change objectMaxDepth when undefined is supplied', function() { - errorHandlingConfig({objectMaxDepth: undefined}); - expect(errorHandlingConfig().objectMaxDepth).toBe(originalObjectMaxDepthInErrorMessage); + describe('isUrlParameters',function() { + it('should get default isUrlParameters', function() { + expect(errorHandlingConfig().isUrlParameters).toBe(true); + }); + it('should set isUrlParameters', function() { + errorHandlingConfig({isUrlParameters:false}); + expect(errorHandlingConfig().isUrlParameters).toBe(false); + errorHandlingConfig({isUrlParameters:true}); + expect(errorHandlingConfig().isUrlParameters).toBe(true); + }); + it('should not change its value when non-boolean is supplied', function() { + errorHandlingConfig({isUrlParameters:123}); + expect(errorHandlingConfig().isUrlParameters).toBe(originalIsUrlParameters); + }); }); - they('should set objectMaxDepth to NaN when $prop is supplied', - [NaN, null, true, false, -1, 0], function(maxDepth) { - errorHandlingConfig({objectMaxDepth: maxDepth}); - expect(errorHandlingConfig().objectMaxDepth).toBeNaN(); - } - ); }); describe('minErr', function() { @@ -164,5 +185,13 @@ 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 not generate URL query parameters when isUrlParameters is false', function() { + + errorHandlingConfig({isUrlParameters:false}); + expect(testError('acode', 'aproblem', 'a', 'b', 'c').message) + .not.toContain('?p0=a&p1=b&p2=c'); + }); + }); });