File tree Expand file tree Collapse file tree 5 files changed +69
-2
lines changed Expand file tree Collapse file tree 5 files changed +69
-2
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import {Handler} from "./types/Handler";
4
4
import { Token } from "./Token" ;
5
5
import { ServiceIdentifier } from "./types/ServiceIdentifier" ;
6
6
import { ServiceNotFoundError } from "./error/ServiceNotFoundError" ;
7
+ import { MissingProvidedServiceTypeError } from "./error/MissingProvidedServiceTypeError" ;
7
8
8
9
/**
9
10
* Service container.
@@ -88,7 +89,7 @@ export class Container {
88
89
// if service was not found then create a new one and register it
89
90
if ( ! service ) {
90
91
if ( ! type )
91
- throw new Error ( `Cannot determine a class of the requesting service " ${ identifier } "` ) ;
92
+ throw new MissingProvidedServiceTypeError ( identifier ) ;
92
93
93
94
service = { type : type } ;
94
95
this . services . push ( service ) ;
@@ -117,7 +118,7 @@ export class Container {
117
118
118
119
} else { // otherwise simply create a new object instance
119
120
if ( ! type )
120
- throw new Error ( `Cannot determine a class of the requesting service " ${ identifier } "` ) ;
121
+ throw new MissingProvidedServiceTypeError ( identifier ) ;
121
122
122
123
params . unshift ( null ) ;
123
124
service . value = new ( type . bind . apply ( type , params ) ) ( ) ;
Original file line number Diff line number Diff line change 1
1
import { Container } from "../Container" ;
2
2
import { Token } from "../Token" ;
3
+ import { CannotInjectError } from "../error/CannotInjectError" ;
3
4
4
5
/**
5
6
* Injects a service into a class property or constructor parameter.
@@ -40,6 +41,10 @@ export function Inject(typeOrName?: ((type?: any) => Function)|string|Token<any>
40
41
} else {
41
42
identifier = typeOrName ( ) ;
42
43
}
44
+
45
+ if ( identifier === Object )
46
+ throw new CannotInjectError ( target , propertyName ) ;
47
+
43
48
return Container . get < any > ( identifier ) ;
44
49
}
45
50
} ) ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments