Skip to content

Commit 91e239e

Browse files
author
Umed Khudoiberdiev
committed
fixes #42
1 parent d6337e2 commit 91e239e

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

src/Container.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {Handler} from "./types/Handler";
44
import {Token} from "./Token";
55
import {ServiceIdentifier} from "./types/ServiceIdentifier";
66
import {ServiceNotFoundError} from "./error/ServiceNotFoundError";
7+
import {MissingProvidedServiceTypeError} from "./error/MissingProvidedServiceTypeError";
78

89
/**
910
* Service container.
@@ -88,7 +89,7 @@ export class Container {
8889
// if service was not found then create a new one and register it
8990
if (!service) {
9091
if (!type)
91-
throw new Error(`Cannot determine a class of the requesting service "${identifier}"`);
92+
throw new MissingProvidedServiceTypeError(identifier);
9293

9394
service = { type: type };
9495
this.services.push(service);
@@ -117,7 +118,7 @@ export class Container {
117118

118119
} else { // otherwise simply create a new object instance
119120
if (!type)
120-
throw new Error(`Cannot determine a class of the requesting service "${identifier}"`);
121+
throw new MissingProvidedServiceTypeError(identifier);
121122

122123
params.unshift(null);
123124
service.value = new (type.bind.apply(type, params))();

src/decorators/Inject.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Container} from "../Container";
22
import {Token} from "../Token";
3+
import {CannotInjectError} from "../error/CannotInjectError";
34

45
/**
56
* Injects a service into a class property or constructor parameter.
@@ -40,6 +41,10 @@ export function Inject(typeOrName?: ((type?: any) => Function)|string|Token<any>
4041
} else {
4142
identifier = typeOrName();
4243
}
44+
45+
if (identifier === Object)
46+
throw new CannotInjectError(target, propertyName);
47+
4348
return Container.get<any>(identifier);
4449
}
4550
});

src/error/CannotInjectError.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Thrown when DI cannot inject value into property decorated by @Inject decorator.
3+
*/
4+
export class CannotInjectError extends Error {
5+
name = "ServiceNotFoundError";
6+
7+
constructor(target: Object, propertyName: string) {
8+
super(
9+
`Cannot inject value into "${target.constructor.name}.${propertyName}". ` +
10+
`Please make sure you setup reflect-metadata properly and you don't use interfaces without service tokens as injection value.`
11+
);
12+
Object.setPrototypeOf(this, CannotInjectError.prototype);
13+
}
14+
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Thrown when service is registered without type.
3+
*/
4+
export class MissingProvidedServiceTypeError extends Error {
5+
name = "ServiceNotFoundError";
6+
7+
constructor(identifier: any) {
8+
super(`Cannot determine a class of the requesting service "${identifier}"`);
9+
Object.setPrototypeOf(this, MissingProvidedServiceTypeError.prototype);
10+
}
11+
12+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import "reflect-metadata";
2+
import {Container} from "../../../src/Container";
3+
import {Service} from "../../../src/decorators/Service";
4+
import {Inject} from "../../../src/decorators/Inject";
5+
import {expect} from "chai";
6+
7+
describe("github issues > #42 Exception not thrown on missing binding", function() {
8+
9+
beforeEach(() => Container.reset());
10+
11+
it("should work properly", function() {
12+
13+
interface Factory {
14+
create(): void;
15+
}
16+
17+
@Service()
18+
class CoffeeMaker {
19+
20+
@Inject() // This is an incorrect usage of typedi because Factory is an interface
21+
private factory: Factory;
22+
23+
make() {
24+
this.factory.create();
25+
}
26+
27+
}
28+
29+
expect(() => {
30+
Container.get(CoffeeMaker);
31+
}).to.throw(Error);
32+
});
33+
34+
});

0 commit comments

Comments
 (0)