* @description * * This object provides a utility for producing rich Error messages within * AngularJS. It can be called as follows: * * var exampleMinErr = minErr('example'); * throw exampleMinErr('one', 'This {0} is {1}', foo, bar); * * The above creates an instance of minErr in the exampl
(module, ErrorConstructor)
| 97 | */ |
| 98 | |
| 99 | function minErr(module, ErrorConstructor) { |
| 100 | ErrorConstructor = ErrorConstructor || Error; |
| 101 | |
| 102 | var url = 'https://errors.angularjs.org/1.8.0/'; |
| 103 | var regex = url.replace('.', '\\.') + '[\\s\\S]*'; |
| 104 | var errRegExp = new RegExp(regex, 'g'); |
| 105 | |
| 106 | return function() { |
| 107 | var code = arguments[0], |
| 108 | template = arguments[1], |
| 109 | message = '[' + (module ? module + ':' : '') + code + '] ', |
| 110 | templateArgs = sliceArgs(arguments, 2).map(function(arg) { |
| 111 | return toDebugString(arg, minErrConfig.objectMaxDepth); |
| 112 | }), |
| 113 | paramPrefix, i; |
| 114 | |
| 115 | // A minErr message has two parts: the message itself and the url that contains the |
| 116 | // encoded message. |
| 117 | // The message's parameters can contain other error messages which also include error urls. |
| 118 | // To prevent the messages from getting too long, we strip the error urls from the parameters. |
| 119 | |
| 120 | message += template.replace(/\{\d+\}/g, function(match) { |
| 121 | var index = +match.slice(1, -1); |
| 122 | |
| 123 | if (index < templateArgs.length) { |
| 124 | return templateArgs[index].replace(errRegExp, ''); |
| 125 | } |
| 126 | |
| 127 | return match; |
| 128 | }); |
| 129 | |
| 130 | message += '\n' + url + (module ? module + '/' : '') + code; |
| 131 | |
| 132 | if (minErrConfig.urlErrorParamsEnabled) { |
| 133 | for (i = 0, paramPrefix = '?'; i < templateArgs.length; i++, paramPrefix = '&') { |
| 134 | message += paramPrefix + 'p' + i + '=' + encodeURIComponent(templateArgs[i]); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return new ErrorConstructor(message); |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | /* We need to tell ESLint what variables are being exported */ |
| 143 | /* exported |
no test coverage detected