mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-12 22:45:52 +08:00
fix($injector): refactor module loading code and use minErr
This commit is contained in:
@@ -494,37 +494,36 @@ function createInjector(modulesToLoad) {
|
||||
forEach(modulesToLoad, function(module) {
|
||||
if (loadedModules.get(module)) return;
|
||||
loadedModules.put(module, true);
|
||||
if (isString(module)) {
|
||||
var moduleFn = angularModule(module);
|
||||
runBlocks = runBlocks.concat(loadModules(moduleFn.requires)).concat(moduleFn._runBlocks);
|
||||
|
||||
try {
|
||||
try {
|
||||
if (isString(module)) {
|
||||
var moduleFn = angularModule(module);
|
||||
runBlocks = runBlocks.concat(loadModules(moduleFn.requires)).concat(moduleFn._runBlocks);
|
||||
|
||||
for(var invokeQueue = moduleFn._invokeQueue, i = 0, ii = invokeQueue.length; i < ii; i++) {
|
||||
var invokeArgs = invokeQueue[i],
|
||||
provider = providerInjector.get(invokeArgs[0]);
|
||||
|
||||
provider[invokeArgs[1]].apply(provider, invokeArgs[2]);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e.message) e.message += ' from ' + module;
|
||||
throw e;
|
||||
} else if (isFunction(module)) {
|
||||
runBlocks.push(providerInjector.invoke(module));
|
||||
} else if (isArray(module)) {
|
||||
runBlocks.push(providerInjector.invoke(module));
|
||||
} else {
|
||||
assertArgFn(module, 'module');
|
||||
}
|
||||
} else if (isFunction(module)) {
|
||||
try {
|
||||
runBlocks.push(providerInjector.invoke(module));
|
||||
} catch (e) {
|
||||
if (e.message) e.message += ' from ' + module;
|
||||
throw e;
|
||||
} catch (e) {
|
||||
if (isArray(module)) {
|
||||
module = module[module.length - 1];
|
||||
}
|
||||
} else if (isArray(module)) {
|
||||
try {
|
||||
runBlocks.push(providerInjector.invoke(module));
|
||||
} catch (e) {
|
||||
if (e.message) e.message += ' from ' + String(module[module.length - 1]);
|
||||
throw e;
|
||||
if (e.message && e.stack && e.stack.indexOf(e.message) == -1) {
|
||||
// Safari & FF's stack traces don't contain error.message content unlike those of Chrome and IE
|
||||
// So if stack doesn't contain message, we create a new string that contains both.
|
||||
// Since error.stack is read-only in Safari, I'm overriding e and not e.stack here.
|
||||
e = e.message + '\n' + e.stack;
|
||||
}
|
||||
} else {
|
||||
assertArgFn(module, 'module');
|
||||
throw $injectorMinErr('modulerr', "Failed to instantiate module {0} due to:\n{1}", module, e.stack || e.message || e);
|
||||
}
|
||||
});
|
||||
return runBlocks;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
/**
|
||||
* @description
|
||||
*
|
||||
* This object provides a utility for producing rich Error messages within
|
||||
* This object provides a utility for producing rich Error messages within
|
||||
* Angular. It can be called as follows:
|
||||
*
|
||||
* var exampleMinErr = minErr('example');
|
||||
@@ -34,14 +34,14 @@ function minErr(module) {
|
||||
template = arguments[1],
|
||||
templateArgs = arguments,
|
||||
message;
|
||||
|
||||
|
||||
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 (isFunction(arg)) {
|
||||
return arg.toString().replace(/ \{[\s\S]*$/, '');
|
||||
return arg.toString().replace(/ ?\{[\s\S]*$/, '');
|
||||
} else if (isUndefined(arg)) {
|
||||
return 'undefined';
|
||||
} else if (!isString(arg)) {
|
||||
|
||||
@@ -605,7 +605,9 @@ describe('angular', function() {
|
||||
|
||||
expect(function() {
|
||||
angularInit(appElement, bootstrap);
|
||||
}).toThrow("[$injector:nomod] Module 'doesntexist' is not available! You either misspelled the module name or forgot to load it.");
|
||||
}).toThrowMatching(
|
||||
/\[\$injector:modulerr] Failed to instantiate module doesntexist due to:\n.*\[\$injector:nomod] Module 'doesntexist' is not available! You either misspelled the module name or forgot to load it\./
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -749,7 +751,8 @@ describe('angular', function() {
|
||||
|
||||
expect(function() {
|
||||
angular.bootstrap(element, ['doesntexist']);
|
||||
}).toThrow("[$injector:nomod] Module 'doesntexist' is not available! You either misspelled the module name or forgot to load it.");
|
||||
}).toThrowMatching(
|
||||
/\[\$injector:modulerr\] Failed to instantiate module doesntexist due to:\n.*\[\$injector:nomod\] Module 'doesntexist' is not available! You either misspelled the module name or forgot to load it\./);
|
||||
|
||||
expect(element.html()).toBe('{{1+2}}');
|
||||
dealoc(element);
|
||||
|
||||
@@ -268,8 +268,9 @@ describe('injector', function() {
|
||||
it('should error on invalid module name', function() {
|
||||
expect(function() {
|
||||
createInjector(['IDontExist'], {});
|
||||
}).toThrow("[$injector:nomod] Module 'IDontExist' is not available! You either misspelled the module name or forgot to load it.");
|
||||
|
||||
}).toThrowMatching(
|
||||
/\[\$injector:modulerr\].+\n.*\[\$injector:nomod] Module 'IDontExist' is not available! You either misspelled the module name or forgot to load it/
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -552,7 +553,7 @@ describe('injector', function() {
|
||||
createInjector([
|
||||
{}
|
||||
], {});
|
||||
}).toThrow("[ng:areq] Argument 'module' is not a function, got Object");
|
||||
}).toThrowMatching(/\[\$injector:modulerr\] Failed to instantiate module {} due to:\n.*\[ng\:areq] Argument 'module' is not a function, got Object/);
|
||||
});
|
||||
|
||||
|
||||
@@ -561,15 +562,17 @@ describe('injector', function() {
|
||||
createInjector([function() {
|
||||
throw 'MyError';
|
||||
}], {});
|
||||
}).toThrow('MyError');
|
||||
}).toThrowMatching(/\[\$injector:modulerr\] Failed to instantiate module .+ due to:\n.*MyError/);
|
||||
});
|
||||
|
||||
|
||||
it('should decorate the missing service error with module name', function() {
|
||||
angular.module('TestModule', [], function(xyzzy) {});
|
||||
expect(function() {
|
||||
createInjector(['TestModule']);
|
||||
}).toThrow('[$injector:unpr] Unknown provider: xyzzy from TestModule');
|
||||
createInjector(['TestModule' ]);
|
||||
}).toThrowMatching(
|
||||
/\[\$injector:modulerr\] Failed to instantiate module TestModule due to:\n.*\[\$injector:unpr] Unknown provider: xyzzy/
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -577,7 +580,9 @@ describe('injector', function() {
|
||||
function myModule(xyzzy){}
|
||||
expect(function() {
|
||||
createInjector([myModule]);
|
||||
}).toThrow('[$injector:unpr] Unknown provider: xyzzy from ' + myModule);
|
||||
}).toThrowMatching(
|
||||
/\[\$injector:modulerr\] Failed to instantiate module function myModule\(xyzzy\) due to:\n.*\[\$injector:unpr] Unknown provider: xyzzy/
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -585,7 +590,9 @@ describe('injector', function() {
|
||||
function myModule(xyzzy){}
|
||||
expect(function() {
|
||||
createInjector([['xyzzy', myModule]]);
|
||||
}).toThrow('[$injector:unpr] Unknown provider: xyzzy from ' + myModule);
|
||||
}).toThrowMatching(
|
||||
/\[\$injector:modulerr\] Failed to instantiate module function myModule\(xyzzy\) due to:\n.*\[\$injector:unpr] Unknown provider: xyzzy/
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -801,7 +808,7 @@ describe('injector', function() {
|
||||
createInjector([function($provide) {
|
||||
$provide.value('name', 'angular')
|
||||
}, instanceLookupInModule]);
|
||||
}).toThrow('[$injector:unpr] Unknown provider: name from ' + String(instanceLookupInModule));
|
||||
}).toThrowMatching(/\[\$injector:unpr] Unknown provider: name/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user