diff --git a/src/.eslintrc.json b/src/.eslintrc.json index 64e8b866e6c8..4bfa57a22bf4 100644 --- a/src/.eslintrc.json +++ b/src/.eslintrc.json @@ -120,6 +120,7 @@ /* minErr.js */ "minErr": false, + "noMinErr": false, /* loader.js */ "setupModuleLoader": false, diff --git a/src/auto/injector.js b/src/auto/injector.js index b4995af8f6f6..eb8b011462da 100644 --- a/src/auto/injector.js +++ b/src/auto/injector.js @@ -890,7 +890,7 @@ function createInjector(modulesToLoad, strictDi) { if (cache[serviceName] === INSTANTIATING) { delete cache[serviceName]; } - throw err; + throw noMinErr('', '', err); } finally { path.shift(); } diff --git a/src/minErr.js b/src/minErr.js index e20040319222..2a09d45b5645 100644 --- a/src/minErr.js +++ b/src/minErr.js @@ -79,7 +79,6 @@ function isValidObjectMaxDepth(maxDepth) { * error from returned function, for cases when a particular type of error is useful. * @returns {function(code:string, template:string, ...templateArgs): Error} minErr instance */ - function minErr(module, ErrorConstructor) { ErrorConstructor = ErrorConstructor || Error; return function() { @@ -111,3 +110,38 @@ function minErr(module, ErrorConstructor) { return new ErrorConstructor(message); }; } + +/** + * @description + * + * In certain case (e.g. when catching and rethrowing an error), it is neither desirable nor + * necessary to pass the error through `minErr()`. You can use this function to avoid warnings + * produced by `ng-closire-runner` during `grunt minall`. + * + * Due to what arguments `ng-closure-runner` expects, the first two arguments must be static + * strings. Therefore, you have to pass the actual error as 3rd argument (see example below). + * + * **WARNING** + * Only use this function when you are certain that the thrown error should NOT be a `minErr` + * instance; + * + * Example usage: + * + * ```js + * try { + * tryAndFail(); + * } catch (err) { + * doSomeThing(err); + * throw noMinErr('', '', err); // Functionally equivalent to `throw err`, + * // but avoids `ng-closure-runner` warnings. + * } + * ``` + * + * @param {string} ignoredCode - Ignored, but necessary for `ng-closure-runner`. + * @param {string} ignoredTemplate - Ignored, but necessary for `ng-closure-runner`. + * @param {*} error - The error object that will be returned. + * @returns {*} - The passed in error. + */ +function noMinErr(ignoredCode, ignoredTemplate, err) { + return err; +} diff --git a/src/ng/compile.js b/src/ng/compile.js index 6ae2722a6fde..58f7f6f7e52b 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1566,7 +1566,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { // Reset the queue to trigger a new schedule next time there is a change onChangesQueue = undefined; if (errors.length) { - throw errors; + throw noMinErr('', '', errors); } }); } finally { diff --git a/src/ng/directive/ngModelOptions.js b/src/ng/directive/ngModelOptions.js index 02a328ff3748..6e6ca8026bf1 100644 --- a/src/ng/directive/ngModelOptions.js +++ b/src/ng/directive/ngModelOptions.js @@ -41,7 +41,7 @@ ModelOptions.prototype = { options = extend({}, options); // Inherit options from the parent if specified by the value `"$inherit"` - forEach(options, /* @this */ function(option, key) { + forEach(options, /** @this */ function(option, key) { if (option === '$inherit') { if (key === '*') { inheritAll = true; diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index 7e4cb6d75680..ec625fddaf94 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -147,7 +147,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc // The json response type can be ignored if not supported, because JSON payloads are // parsed on the client-side regardless. if (responseType !== 'json') { - throw e; + throw noMinErr('', '', e); } } } diff --git a/src/ng/location.js b/src/ng/location.js index 09f08c09cdfe..a938d8cd1164 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -894,7 +894,7 @@ function $LocationProvider() { $location.url(oldUrl); $location.$$state = oldState; - throw e; + throw noMinErr('', '', e); } } diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 7f37338e6636..ffe7d853e50a 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -1192,7 +1192,7 @@ function $RootScopeProvider() { } catch (e) { $exceptionHandler(e); // eslint-disable-next-line no-unsafe-finally - throw e; + throw noMinErr('', '', e); } } }, diff --git a/test/.eslintrc.json b/test/.eslintrc.json index 6401cb26f590..fa38d1a6673e 100644 --- a/test/.eslintrc.json +++ b/test/.eslintrc.json @@ -117,6 +117,7 @@ /* minerr.js */ "minErr": false, + "noMinErr": false, /* loader.js */ "setupModuleLoader": false, diff --git a/test/minErrSpec.js b/test/minErrSpec.js index aae001cba415..dcf2ee30d45f 100644 --- a/test/minErrSpec.js +++ b/test/minErrSpec.js @@ -166,3 +166,13 @@ describe('errors', function() { }); }); }); + +describe('noMinErr', function() { + it('should return the 3rd argument', function() { + expect(noMinErr('foo', 'bar', 'baz')).toBe('baz'); + expect(noMinErr('foo', 'bar', null)).toBe(null); + expect(noMinErr('foo', 'bar')).toBeUndefined(); + expect(noMinErr('foo')).toBeUndefined(); + expect(noMinErr()).toBeUndefined(); + }); +});