mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-05-07 23:17:02 +08:00
In some cases, the type of Error thrown by minErr is meaningful, such as in $q where a TypeError is sometimes required. This fix allows providing an error constructor as the second argument to minErr, which will be used to construct the error that gets returned by the factory function.
78 lines
2.9 KiB
JavaScript
78 lines
2.9 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* @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 namespace. The
|
|
* resulting error will have a namespaced error code of example.one. The
|
|
* resulting error will replace {0} with the value of foo, and {1} with the
|
|
* value of bar. The object is not restricted in the number of arguments it can
|
|
* take.
|
|
*
|
|
* If fewer arguments are specified than necessary for interpolation, the extra
|
|
* interpolation markers will be preserved in the final string.
|
|
*
|
|
* Since data will be parsed statically during a build step, some restrictions
|
|
* are applied with respect to how minErr instances are created and called.
|
|
* Instances should have names of the form namespaceMinErr for a minErr created
|
|
* using minErr('namespace') . Error codes, namespaces and template strings
|
|
* should all be static strings, not variables or general expressions.
|
|
*
|
|
* @param {string} module The namespace to use for the new minErr instance.
|
|
* @param {function} ErrorConstructor Custom error constructor to be instantiated when returning
|
|
* 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 () {
|
|
var code = arguments[0],
|
|
prefix = '[' + (module ? module + ':' : '') + code + '] ',
|
|
template = arguments[1],
|
|
templateArgs = arguments,
|
|
stringify = function (obj) {
|
|
if (typeof obj === 'function') {
|
|
return obj.toString().replace(/ \{[\s\S]*$/, '');
|
|
} else if (typeof obj === 'undefined') {
|
|
return 'undefined';
|
|
} else if (typeof obj !== 'string') {
|
|
return JSON.stringify(obj);
|
|
}
|
|
return obj;
|
|
},
|
|
message, i;
|
|
|
|
message = prefix + template.replace(/\{\d+\}/g, function (match) {
|
|
var index = +match.slice(1, -1), arg;
|
|
|
|
if (index + 2 < templateArgs.length) {
|
|
arg = templateArgs[index + 2];
|
|
if (typeof arg === 'function') {
|
|
return arg.toString().replace(/ ?\{[\s\S]*$/, '');
|
|
} else if (typeof arg === 'undefined') {
|
|
return 'undefined';
|
|
} else if (typeof arg !== 'string') {
|
|
return toJson(arg);
|
|
}
|
|
return arg;
|
|
}
|
|
return match;
|
|
});
|
|
|
|
message = message + '\nhttp://errors.angularjs.org/"NG_VERSION_FULL"/' +
|
|
(module ? module + '/' : '') + code;
|
|
for (i = 2; i < arguments.length; i++) {
|
|
message = message + (i == 2 ? '?' : '&') + 'p' + (i-2) + '=' +
|
|
encodeURIComponent(stringify(arguments[i]));
|
|
}
|
|
return new ErrorConstructor(message);
|
|
};
|
|
}
|