Skip to content

Commit 6eec601

Browse files
Merge pull request nestjs#1143 from BrunnerLivio/feature/print-context-injection-error
feature(core) add module context name to UnknownDependenciesMessage
2 parents 9b799ba + 4e943db commit 6eec601

5 files changed

Lines changed: 44 additions & 8 deletions

File tree

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { InjectorDependencyContext } from '../../injector/injector';
22
import { UNKNOWN_DEPENDENCIES_MESSAGE } from '../messages';
33
import { RuntimeException } from './runtime.exception';
4+
import { Module } from '../../injector/module';
45

56
export class UndefinedDependencyException extends RuntimeException {
67
constructor(
78
type: string,
89
undefinedDependencyContext: InjectorDependencyContext,
10+
module?: Module,
911
) {
10-
super(UNKNOWN_DEPENDENCIES_MESSAGE(type, undefinedDependencyContext));
12+
super(UNKNOWN_DEPENDENCIES_MESSAGE(type, undefinedDependencyContext, module));
1113
}
1214
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { InjectorDependencyContext } from '../../injector/injector';
22
import { UNKNOWN_DEPENDENCIES_MESSAGE } from '../messages';
33
import { RuntimeException } from './runtime.exception';
4+
import { Module } from '../../injector/module';
45

56
export class UnknownDependenciesException extends RuntimeException {
67
constructor(
78
type: string,
89
unknownDependencyContext: InjectorDependencyContext,
10+
module?: Module,
911
) {
10-
super(UNKNOWN_DEPENDENCIES_MESSAGE(type, unknownDependencyContext));
12+
super(UNKNOWN_DEPENDENCIES_MESSAGE(type, unknownDependencyContext, module));
1113
}
1214
}

packages/core/errors/messages.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,34 @@ import {
33
InjectorDependency,
44
InjectorDependencyContext,
55
} from '../injector/injector';
6+
import { Module } from '../injector/module';
7+
8+
// TODO: Replace `any` with `unknown` type when TS 3.0.0 is supported
9+
/**
10+
* Returns the name of an instance
11+
* @param instance The instance which should get the name from
12+
*/
13+
const getInstanceName = (instance: any) =>
14+
(instance && (instance as Type<any>).name);
615

716
/**
817
* Returns the name of the dependency
918
* Tries to get the class name, otherwise the string value
1019
* (= injection token). As fallback it returns '+'
11-
* @param dependency The dependency whichs name shoul get displayed
20+
* @param dependency The dependency whichs name should get displayed
21+
*/
22+
const getDependencyName = (dependency: InjectorDependency) => getInstanceName(dependency) || dependency || '+';
23+
/**
24+
* Returns the name of the module
25+
* Tries to get the class name. As fallback it returns 'current'.
26+
* @param module The module which should get displayed
1227
*/
13-
const getDependencyName = (dependency: InjectorDependency) =>
14-
(dependency && (dependency as Type<any>).name) || dependency || '+';
28+
const getModuleName = (module: Module) => (module && getInstanceName(module.metatype)) || 'current';
1529

1630
export const UNKNOWN_DEPENDENCIES_MESSAGE = (
1731
type: string,
1832
unknownDependencyContext: InjectorDependencyContext,
33+
module: Module,
1934
) => {
2035
const { index, dependencies } = unknownDependencyContext;
2136
let message = `Nest can't resolve dependencies of the ${type}`;
@@ -25,7 +40,7 @@ export const UNKNOWN_DEPENDENCIES_MESSAGE = (
2540
dependenciesName[index] = '?';
2641
message += dependenciesName.join(', ');
2742

28-
message += `). Please make sure that the argument at index [${index}] is available in the current context.`;
43+
message += `). Please make sure that the argument at index [${index}] is available in the ${getModuleName(module)} context.`;
2944
return message;
3045
};
3146

packages/core/injector/injector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class Injector {
207207
module: Module,
208208
) {
209209
if (isUndefined(param)) {
210-
throw new UndefinedDependencyException(wrapper.name, dependencyContext);
210+
throw new UndefinedDependencyException(wrapper.name, dependencyContext, module);
211211
}
212212
const token = this.resolveParamToken(wrapper, param);
213213
return this.resolveComponentInstance<T>(
@@ -273,7 +273,7 @@ export class Injector {
273273
dependencyContext.name,
274274
);
275275
if (isNil(instanceWrapper)) {
276-
throw new UnknownDependenciesException(wrapper.name, dependencyContext);
276+
throw new UnknownDependenciesException(wrapper.name, dependencyContext, module);
277277
}
278278
return instanceWrapper;
279279
}

packages/core/test/errors/test/messages.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from 'chai';
22
import { UnknownDependenciesException } from '../../../errors/exceptions/unknown-dependencies.exception';
3+
import { Module } from '../../../injector/module';
34

45
describe('UnknownDependenciesMessage', () => {
56
const index = 0;
@@ -29,4 +30,20 @@ describe('UnknownDependenciesMessage', () => {
2930
'Please make sure that the argument at index [0] is available in the current context.';
3031
expect(new UnknownDependenciesException('CatService', { index, dependencies: ['', undefined] }).message).to.equal(expectedResult);
3132
});
33+
it('should display the module name', () => {
34+
const expectedResult =
35+
'Nest can\'t resolve dependencies of the CatService (?, MY_TOKEN). ' +
36+
'Please make sure that the argument at index [0] is available in the TestModule context.';
37+
class MetaType {
38+
name: string;
39+
}
40+
class TestModule {
41+
metatype: MetaType;
42+
}
43+
const myModule = new TestModule();
44+
const myMetaType = new MetaType();
45+
myMetaType.name = 'TestModule';
46+
myModule.metatype = myMetaType;
47+
expect(new UnknownDependenciesException('CatService', { index, dependencies: ['', 'MY_TOKEN'] }, myModule as Module).message).to.equal(expectedResult);
48+
});
3249
});

0 commit comments

Comments
 (0)