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

fix($injector): throw when factory $get method does not return a value #9210

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/content/error/$injector/undef.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@ngdoc error
@name $injector:undef
@fullName Undefined Value
@description

This error results from registering a factory which does not return a value (or whose return value is undefined).

The following is an example of a factory which will throw this error upon injection:

```js
angular.module("badModule", []).
factory("badFactory", function() {
doLotsOfThings();
butDontReturnAValue();
});
```

In order to prevent the error, return a value of some sort, such as an object which exposes an API for working
with the injected object.

```js
angular.module("goodModule", []).
factory("goodFactory", function() {
doLotsOfThings();
butDontReturnAValue();

return {
doTheThing: function methodThatDoesAThing() {
}
};
});
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing EOL

18 changes: 16 additions & 2 deletions src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,15 +663,29 @@ function createInjector(modulesToLoad, strictDi) {
return providerCache[name + providerSuffix] = provider_;
}

function factory(name, factoryFn) { return provider(name, { $get: factoryFn }); }
function enforceReturnValue(name, factory) {
return function enforcedReturnValue() {
var result = instanceInjector.invoke(factory);
if (isUndefined(result)) {
throw $injectorMinErr('undef', "Provider '{0}' must return a value from $get factory method.", name);
}
return result;
};
}

function factory(name, factoryFn, enforce) {
return provider(name, {
$get: enforce !== false ? enforceReturnValue(name, factoryFn) : factoryFn
});
}

function service(name, constructor) {
return factory(name, ['$injector', function($injector) {
return $injector.instantiate(constructor);
}]);
}

function value(name, val) { return factory(name, valueFn(val)); }
function value(name, val) { return factory(name, valueFn(val), false); }

function constant(name, value) {
assertNotHasOwnProperty(name, 'constant');
Expand Down
22 changes: 16 additions & 6 deletions test/auto/injectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ describe('injector', function() {
// s6


providers('s1', function() { log.push('s1'); }, {$inject: ['s2', 's5', 's6']});
providers('s2', function() { log.push('s2'); }, {$inject: ['s3', 's4', 's5']});
providers('s3', function() { log.push('s3'); }, {$inject: ['s6']});
providers('s4', function() { log.push('s4'); }, {$inject: ['s3', 's5']});
providers('s5', function() { log.push('s5'); });
providers('s6', function() { log.push('s6'); });
providers('s1', function() { log.push('s1'); return {}; }, {$inject: ['s2', 's5', 's6']});
providers('s2', function() { log.push('s2'); return {}; }, {$inject: ['s3', 's4', 's5']});
providers('s3', function() { log.push('s3'); return {}; }, {$inject: ['s6']});
providers('s4', function() { log.push('s4'); return {}; }, {$inject: ['s3', 's5']});
providers('s5', function() { log.push('s5'); return {}; });
providers('s6', function() { log.push('s6'); return {}; });

injector.get('s1');

Expand Down Expand Up @@ -983,4 +983,14 @@ describe('strict-di injector', function() {
}).toThrowMinErr('$injector', 'strictdi');
});
});


it('should throw if factory does not return a value', function() {
module(function($provide) {
$provide.factory('$test', function() {});
});
expect(function() {
inject(function($test) {});
}).toThrowMinErr('$injector', 'undef');
});
});