Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 003861d

Browse files
ksheedloIgorMinar
authored andcommitted
chore(minErr): replace ngError with minErr
1 parent 908071a commit 003861d

35 files changed

+198
-177
lines changed

angularFiles.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
angularFiles = {
22
'angularSrc': [
3+
'src/minErr.js',
34
'src/Angular.js',
45
'src/loader.js',
56
'src/AngularPublic.js',
67
'src/jqLite.js',
78
'src/apis.js',
8-
'src/ngError.js',
99

1010
'src/auto/injector.js',
1111

src/Angular.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var /** holds major version number for IE or NaN for real browsers */
5454
slice = [].slice,
5555
push = [].push,
5656
toString = Object.prototype.toString,
57+
ngMinErr = minErr('ng'),
5758

5859

5960
_angular = window.angular,
@@ -573,7 +574,7 @@ function isLeafNode (node) {
573574
*/
574575
function copy(source, destination){
575576
if (isWindow(source) || isScope(source)) {
576-
throw ngError(43, "Can't copy! Making copies of Window or Scope instances is not supported.");
577+
throw ngMinErr('cpws', "Can't copy! Making copies of Window or Scope instances is not supported.");
577578
}
578579

579580
if (!destination) {
@@ -588,7 +589,7 @@ function copy(source, destination){
588589
}
589590
}
590591
} else {
591-
if (source === destination) throw ngError(44, "Can't copy! Source and destination are identical.");
592+
if (source === destination) throw ngMinErr('cpi', "Can't copy! Source and destination are identical.");
592593
if (isArray(source)) {
593594
destination.length = 0;
594595
for ( var i = 0; i < source.length; i++) {
@@ -1044,7 +1045,7 @@ function bindJQuery() {
10441045
*/
10451046
function assertArg(arg, name, reason) {
10461047
if (!arg) {
1047-
throw ngError(45, "Argument '{0}' is {1}", (name || '?'), (reason || "required"));
1048+
throw ngMinErr('areq', "Argument '{0}' is {1}", (name || '?'), (reason || "required"));
10481049
}
10491050
return arg;
10501051
}

src/auto/injector.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
4242
var FN_ARG_SPLIT = /,/;
4343
var FN_ARG = /^\s*(_?)(\S+?)\1\s*$/;
4444
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
45+
var $injectorMinErr = minErr('$injector');
4546
function annotate(fn) {
4647
var $inject,
4748
fnText,
@@ -422,7 +423,7 @@ function createInjector(modulesToLoad) {
422423
},
423424
providerInjector = (providerCache.$injector =
424425
createInternalInjector(providerCache, function() {
425-
throw ngError(1, "Unknown provider: {0}", path.join(' <- '));
426+
throw $injectorMinErr('unpr', "Unknown provider: {0}", path.join(' <- '));
426427
})),
427428
instanceCache = {},
428429
instanceInjector = (instanceCache.$injector =
@@ -455,7 +456,7 @@ function createInjector(modulesToLoad) {
455456
provider_ = providerInjector.instantiate(provider_);
456457
}
457458
if (!provider_.$get) {
458-
throw ngError(2, "Provider '{0}' must define $get factory method.", name);
459+
throw $injectorMinErr('pget', "Provider '{0}' must define $get factory method.", name);
459460
}
460461
return providerCache[name + providerSuffix] = provider_;
461462
}
@@ -538,7 +539,7 @@ function createInjector(modulesToLoad) {
538539
function getService(serviceName) {
539540
if (cache.hasOwnProperty(serviceName)) {
540541
if (cache[serviceName] === INSTANTIATING) {
541-
throw ngError(4, 'Circular dependency found: {0}', path.join(' <- '));
542+
throw $injectorMinErr('cdep', 'Circular dependency found: {0}', path.join(' <- '));
542543
}
543544
return cache[serviceName];
544545
} else {
@@ -561,7 +562,7 @@ function createInjector(modulesToLoad) {
561562
for(i = 0, length = $inject.length; i < length; i++) {
562563
key = $inject[i];
563564
if (typeof key !== 'string') {
564-
throw ngError(3, 'Incorrect injection token! Expected service name as string, got {0}', key);
565+
throw $injectorMinErr('itkn', 'Incorrect injection token! Expected service name as string, got {0}', key);
565566
}
566567
args.push(
567568
locals && locals.hasOwnProperty(key)

src/jqLite.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function JQLite(element) {
153153
}
154154
if (!(this instanceof JQLite)) {
155155
if (isString(element) && element.charAt(0) != '<') {
156-
throw ngError(46, 'Looking up elements via selectors is not supported by jqLite! See: http://docs.angularjs.org/api/angular.element');
156+
throw minErr('jqLite')('nosel', 'Looking up elements via selectors is not supported by jqLite! See: http://docs.angularjs.org/api/angular.element');
157157
}
158158
return new JQLite(element);
159159
}

src/loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function setupModuleLoader(window) {
7070
}
7171
return ensure(modules, name, function() {
7272
if (!requires) {
73-
throw ngError(47, "Module '{0}' is not available! You either misspelled the module name or forgot to load it.", name);
73+
throw minErr('$injector')('nomod', "Module '{0}' is not available! You either misspelled the module name or forgot to load it.", name);
7474
}
7575

7676
/** @type {!Array.<Array.<*>>} */

src/minErr.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
/**
4+
* @description
5+
*
6+
* This object provides a utility for producing rich Error messages within
7+
* Angular. It can be called as follows:
8+
*
9+
* var exampleMinErr = minErr('example');
10+
* throw exampleMinErr('one', 'This {0} is {1}', foo, bar);
11+
*
12+
* The above creates an instance of minErr in the example namespace. The
13+
* resulting error will have a namespaced error code of example.one. The
14+
* resulting error will replace {0} with the value of foo, and {1} with the
15+
* value of bar. The object is not restricted in the number of arguments it can
16+
* take.
17+
*
18+
* If fewer arguments are specified than necessary for interpolation, the extra
19+
* interpolation markers will be preserved in the final string.
20+
*
21+
* Since data will be parsed statically during a build step, some restrictions
22+
* are applied with respect to how minErr instances are created and called.
23+
* Instances should have names of the form namespaceMinErr for a minErr created
24+
* using minErr('namespace') . Error codes, namespaces and template strings
25+
* should all be static strings, not variables or general expressions.
26+
*
27+
* @param {string} module The namespace to use for the new minErr instance.
28+
* @returns {function(string, string, ...): Error} instance
29+
*/
30+
31+
function minErr(module) {
32+
return function () {
33+
var prefix = '[' + (module ? module + ':' : '') + arguments[0] + '] ',
34+
template = arguments[1],
35+
templateArgs = arguments,
36+
message;
37+
38+
message = prefix + template.replace(/\{\d+\}/g, function (match) {
39+
var index = +match.slice(1, -1), arg;
40+
41+
if (index + 2 < templateArgs.length) {
42+
arg = templateArgs[index + 2];
43+
if (isFunction(arg)) {
44+
return arg.toString().replace(/ \{[\s\S]*$/, '');
45+
} else if (isUndefined(arg)) {
46+
return 'undefined';
47+
} else if (!isString(arg)) {
48+
return toJson(arg);
49+
}
50+
return arg;
51+
}
52+
return match;
53+
});
54+
55+
return new Error(message);
56+
};
57+
}

src/ng/cacheFactory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function $CacheFactoryProvider() {
2828

2929
function cacheFactory(cacheId, options) {
3030
if (cacheId in caches) {
31-
throw ngError(10, "CacheId '{0}' is already taken!", cacheId);
31+
throw minErr('$cacheFactory')('iid', "CacheId '{0}' is already taken!", cacheId);
3232
}
3333

3434
var size = 0,

src/ng/compile.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
* {@link guide/compiler Angular HTML Compiler} section of the Developer Guide.
139139
*/
140140

141+
var $compileMinErr = minErr('$compile');
141142

142143
/**
143144
* @ngdoc service
@@ -589,7 +590,7 @@ function $CompileProvider($provide) {
589590
var startNode = node;
590591
do {
591592
if (!node) {
592-
throw ngError(51, "Unterminated attribute, found '{0}' but no matching '{1}' found.", attrStart, attrEnd);
593+
throw $compileMinErr('utrat', "Unterminated attribute, found '{0}' but no matching '{1}' found.", attrStart, attrEnd);
593594
}
594595
if (node.nodeType == 1 /** Element **/) {
595596
if (node.hasAttribute(attrStart)) depth++;
@@ -721,7 +722,7 @@ function $CompileProvider($provide) {
721722
compileNode = $template[0];
722723

723724
if ($template.length != 1 || compileNode.nodeType !== 1) {
724-
throw ngError(12, "Template for directive '{0}' must have exactly one root element.", directiveName);
725+
throw $compileMinErr('tplrt', "Template for directive '{0}' must have exactly one root element. {1}", directiveName, '');
725726
}
726727

727728
replaceWith(jqCollection, $compileNode, compileNode);
@@ -809,7 +810,7 @@ function $CompileProvider($provide) {
809810
}
810811
value = $element[retrievalMethod]('$' + require + 'Controller');
811812
if (!value && !optional) {
812-
throw ngError(13, "Controller '{0}', required by directive '{1}', can't be found!", require, directiveName);
813+
throw $compileMinErr('ctreq', "Controller '{0}', required by directive '{1}', can't be found!", require, directiveName);
813814
}
814815
return value;
815816
} else if (isArray(require)) {
@@ -869,7 +870,7 @@ function $CompileProvider($provide) {
869870
parentSet = parentGet.assign || function() {
870871
// reset the change, or we will throw this exception on every $digest
871872
lastValue = scope[scopeName] = parentGet(parentScope);
872-
throw ngError(14, "Expression '{0}' used with directive '{1}' is non-assignable!",
873+
throw $compileMinErr('noass', "Expression '{0}' used with directive '{1}' is non-assignable!",
873874
attrs[attrName], newIsolateScopeDirective.name);
874875
};
875876
lastValue = scope[scopeName] = parentGet(parentScope);
@@ -900,7 +901,7 @@ function $CompileProvider($provide) {
900901
}
901902

902903
default: {
903-
throw ngError(15, "Invalid isolate scope definition for directive '{0}'. Definition: {... {1}: '{2}' ...}",
904+
throw $compileMinErr('iscp', "Invalid isolate scope definition for directive '{0}'. Definition: {... {1}: '{2}' ...}",
904905
newIsolateScopeDirective.name, scopeName, definition);
905906
}
906907
}
@@ -1057,7 +1058,7 @@ function $CompileProvider($provide) {
10571058
compileNode = $template[0];
10581059

10591060
if ($template.length != 1 || compileNode.nodeType !== 1) {
1060-
throw ngError(16, "Template for directive '{0}' must have exactly one root element. Template: {1}",
1061+
throw $compileMinErr('tplrt', "Template for directive '{0}' must have exactly one root element. {1}",
10611062
origAsyncDirective.name, templateUrl);
10621063
}
10631064

@@ -1095,7 +1096,7 @@ function $CompileProvider($provide) {
10951096
linkQueue = null;
10961097
}).
10971098
error(function(response, code, headers, config) {
1098-
throw ngError(17, 'Failed to load template: {0}', config.url);
1099+
throw $compileMinErr('tpload', 'Failed to load template: {0}', config.url);
10991100
});
11001101

11011102
return function delayedNodeLinkFn(ignoreChildLinkFn, scope, node, rootElement, controller) {
@@ -1123,7 +1124,7 @@ function $CompileProvider($provide) {
11231124

11241125
function assertNoDuplicate(what, previousDirective, directive, element) {
11251126
if (previousDirective) {
1126-
throw ngError(18, 'Multiple directives [{0}, {1}] asking for {2} on: {3}',
1127+
throw $compileMinErr('multidir', 'Multiple directives [{0}, {1}] asking for {2} on: {3}',
11271128
previousDirective.name, directive.name, what, startingTag(element));
11281129
}
11291130
}

src/ng/controller.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function $ControllerProvider() {
7575

7676
if (identifier) {
7777
if (!(locals && typeof locals.$scope == 'object')) {
78-
throw ngError(47, "Cannot export controller '{0}' as '{1}'! No $scope object provided via `locals`.", constructor || expression.name, identifier);
78+
throw minErr('$controller')('noscp', "Cannot export controller '{0}' as '{1}'! No $scope object provided via `locals`.", constructor || expression.name, identifier);
7979
}
8080

8181
locals.$scope[identifier] = instance;

src/ng/directive/input.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,9 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
475475
var patternObj = scope.$eval(pattern);
476476

477477
if (!patternObj || !patternObj.test) {
478-
throw ngError(5, 'ngPattern error! Expected {0} to be a RegExp but was {1}. Element: {2}',
479-
pattern, patternObj, startingTag(element));
478+
throw minErr('ngPattern')('noregexp',
479+
'Expected {0} to be a RegExp but was {1}. Element: {2}', pattern,
480+
patternObj, startingTag(element));
480481
}
481482
return validate(patternObj, value);
482483
};
@@ -928,8 +929,8 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
928929
ngModelSet = ngModelGet.assign;
929930

930931
if (!ngModelSet) {
931-
throw ngError(6, "ngModel error! Expression '{0}' is non-assignable. Element: {1}", $attr.ngModel,
932-
startingTag($element));
932+
throw minErr('ngModel')('noass', "Expression '{0}' is non-assignable. Element: {1}",
933+
$attr.ngModel, startingTag($element));
933934
}
934935

935936
/**

src/ng/directive/ngRepeat.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
*/
192192
var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
193193
var NG_REMOVED = '$$NG_REMOVED';
194+
var ngRepeatMinErr = minErr('ngRepeat');
194195
return {
195196
transclude: 'element',
196197
priority: 1000,
@@ -204,7 +205,7 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
204205
hashFnLocals = {$id: hashKey};
205206

206207
if (!match) {
207-
throw ngError(7, "ngRepeat error! Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.",
208+
throw ngRepeatMinErr('iexp', "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.",
208209
expression);
209210
}
210211

@@ -229,7 +230,7 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
229230

230231
match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/);
231232
if (!match) {
232-
throw ngError(8, "ngRepeat error! '_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'.",
233+
throw ngRepeatMinErr('iidexp', "'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'.",
233234
lhs);
234235
}
235236
valueIdentifier = match[3] || match[1];
@@ -291,7 +292,7 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
291292
if (block && block.startNode) lastBlockMap[block.id] = block;
292293
});
293294
// This is a duplicate and we need to throw an error
294-
throw ngError(50, "ngRepeat error! Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}",
295+
throw ngRepeatMinErr('dupes', "Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}",
295296
expression, trackById);
296297
} else {
297298
// new never before seen block

src/ng/directive/select.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
300300
var match;
301301

302302
if (! (match = optionsExp.match(NG_OPTIONS_REGEXP))) {
303-
throw ngError(9,
304-
"ngOptions error! Expected expression in form of '_select_ (as _label_)? for (_key_,)?_value_ in _collection_' but got '{0}'. Element: {1}",
303+
throw minErr('ngOptions')('iexp',
304+
"Expected expression in form of '_select_ (as _label_)? for (_key_,)?_value_ in _collection_' but got '{0}'. Element: {1}",
305305
optionsExp, startingTag(selectElement));
306306
}
307307

src/ng/httpBackend.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var XHR = window.XMLHttpRequest || function() {
22
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e1) {}
33
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e2) {}
44
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e3) {}
5-
throw ngError(19, "This browser does not support XMLHttpRequest.");
5+
throw minErr('$httpBackend')('noxhr', "This browser does not support XMLHttpRequest.");
66
};
77

88

src/ng/interpolate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function $InterpolateProvider() {
139139
return concat.join('');
140140
}
141141
catch(err) {
142-
var newErr = ngError(48, "$interpolate error! Can't interpolate: {0}\n{1}", text, err.toString());
142+
var newErr = minErr('$interpolate')('interr', "Can't interpolate: {0}\n{1}", text, err.toString());
143143
$exceptionHandler(newErr);
144144
}
145145
};

src/ng/location.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var SERVER_MATCH = /^([^:]+):\/\/(\w+:{0,1}\w*@)?(\{?[\w\.-]*\}?)(:([0-9]+))?(\/[^\?#]*)?(\?([^#]*))?(#(.*))?$/,
44
PATH_MATCH = /^([^\?#]*)(\?([^#]*))?(#(.*))?$/,
55
DEFAULT_PORTS = {'http': 80, 'https': 443, 'ftp': 21};
6+
var $locationMinErr = minErr('$location');
67

78

89
/**
@@ -95,7 +96,7 @@ function LocationHtml5Url(appBase, basePrefix) {
9596
matchUrl(url, parsed);
9697
var pathUrl = beginsWith(appBaseNoFile, url);
9798
if (!isString(pathUrl)) {
98-
throw ngError(21, '$location error! Invalid url "{0}", missing path prefix "{1}".', url, appBaseNoFile);
99+
throw $locationMinErr('nopp', 'Invalid url "{0}", missing path prefix "{1}".', url, appBaseNoFile);
99100
}
100101
matchAppUrl(pathUrl, parsed);
101102
extend(this, parsed);
@@ -157,11 +158,11 @@ function LocationHashbangUrl(appBase, hashPrefix) {
157158
matchUrl(url, this);
158159
var withoutBaseUrl = beginsWith(appBase, url) || beginsWith(appBaseNoFile, url);
159160
if (!isString(withoutBaseUrl)) {
160-
throw ngError(22, '$location error! Invalid url "{0}", does not start with "{1}".', url, appBase);
161+
throw $locationMinErr('istart', 'Invalid url "{0}", does not start with "{1}".', url, appBase);
161162
}
162163
var withoutHashUrl = withoutBaseUrl.charAt(0) == '#' ? beginsWith(hashPrefix, withoutBaseUrl) : withoutBaseUrl;
163164
if (!isString(withoutHashUrl)) {
164-
throw ngError(49, '$location error! Invalid url "{0}", missing hash prefix "{1}".', url, hashPrefix);
165+
throw $locationMinErr('nohash', 'Invalid url "{0}", missing hash prefix "{1}".', url, hashPrefix);
165166
}
166167
matchAppUrl(withoutHashUrl, this);
167168
this.$$compose();

0 commit comments

Comments
 (0)