* @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)
| 34 | */ |
| 35 | |
| 36 | function minErr(module) { |
| 37 | return function () { |
| 38 | var code = arguments[0], |
| 39 | prefix = '[' + (module ? module + ':' : '') + code + '] ', |
| 40 | template = arguments[1], |
| 41 | templateArgs = arguments, |
| 42 | stringify = function (obj) { |
| 43 | if (typeof obj === 'function') { |
| 44 | return obj.toString().replace(/ \{[\s\S]*$/, ''); |
| 45 | } else if (typeof obj === 'undefined') { |
| 46 | return 'undefined'; |
| 47 | } else if (typeof obj !== 'string') { |
| 48 | return JSON.stringify(obj); |
| 49 | } |
| 50 | return obj; |
| 51 | }, |
| 52 | message, i; |
| 53 | |
| 54 | message = prefix + template.replace(/\{\d+\}/g, function (match) { |
| 55 | var index = +match.slice(1, -1), arg; |
| 56 | |
| 57 | if (index + 2 < templateArgs.length) { |
| 58 | arg = templateArgs[index + 2]; |
| 59 | if (typeof arg === 'function') { |
| 60 | return arg.toString().replace(/ ?\{[\s\S]*$/, ''); |
| 61 | } else if (typeof arg === 'undefined') { |
| 62 | return 'undefined'; |
| 63 | } else if (typeof arg !== 'string') { |
| 64 | return toJson(arg); |
| 65 | } |
| 66 | return arg; |
| 67 | } |
| 68 | return match; |
| 69 | }); |
| 70 | |
| 71 | message = message + '\nhttp://errors.angularjs.org/1.2.32/' + |
| 72 | (module ? module + '/' : '') + code; |
| 73 | for (i = 2; i < arguments.length; i++) { |
| 74 | message = message + (i == 2 ? '?' : '&') + 'p' + (i-2) + '=' + |
| 75 | encodeURIComponent(stringify(arguments[i])); |
| 76 | } |
| 77 | |
| 78 | return new Error(message); |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | /* We need to tell jshint what variables are being exported */ |
| 83 | /* global angular: true, |
no test coverage detected