* @description * * This object provides a utility for producing rich Error messages within * Angular. 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 example
(module, ErrorConstructor)
| 36 | */ |
| 37 | |
| 38 | function minErr(module, ErrorConstructor) { |
| 39 | ErrorConstructor = ErrorConstructor || Error; |
| 40 | return function() { |
| 41 | var code = arguments[0], |
| 42 | prefix = '[' + (module ? module + ':' : '') + code + '] ', |
| 43 | template = arguments[1], |
| 44 | templateArgs = arguments, |
| 45 | |
| 46 | message, i; |
| 47 | |
| 48 | message = prefix + template.replace(/\{\d+\}/g, function(match) { |
| 49 | var index = +match.slice(1, -1), arg; |
| 50 | |
| 51 | if (index + 2 < templateArgs.length) { |
| 52 | return toDebugString(templateArgs[index + 2]); |
| 53 | } |
| 54 | return match; |
| 55 | }); |
| 56 | |
| 57 | message = message + '\nhttp://errors.angularjs.org/1.3.20/' + |
| 58 | (module ? module + '/' : '') + code; |
| 59 | for (i = 2; i < arguments.length; i++) { |
| 60 | message = message + (i == 2 ? '?' : '&') + 'p' + (i - 2) + '=' + |
| 61 | encodeURIComponent(toDebugString(arguments[i])); |
| 62 | } |
| 63 | return new ErrorConstructor(message); |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | /* We need to tell jshint what variables are being exported */ |
| 68 | /* global angular: true, |
no test coverage detected