Skip to content

Commit 92325cf

Browse files
committed
feat(errors): update error messaging to provide solutions
1 parent 2a22be7 commit 92325cf

6 files changed

Lines changed: 176 additions & 69 deletions

File tree

package-lock.json

Lines changed: 12 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/errors/exceptions/unknown-export.exception.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { UNKNOWN_EXPORT_MESSAGE } from '../messages';
22
import { RuntimeException } from './runtime.exception';
33

44
export class UnknownExportException extends RuntimeException {
5-
constructor(name: string) {
6-
super(UNKNOWN_EXPORT_MESSAGE`${name}`);
5+
constructor(token: string, module: string) {
6+
super(UNKNOWN_EXPORT_MESSAGE(token, module));
77
}
88
}

packages/core/errors/messages.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,31 @@ export const UNKNOWN_DEPENDENCIES_MESSAGE = (
4343
unknownDependencyContext: InjectorDependencyContext,
4444
module: Module,
4545
) => {
46-
const { index, dependencies, key } = unknownDependencyContext;
47-
let message = `Nest can't resolve dependencies of the ${type.toString()}`;
46+
const { index, name = 'dependency', dependencies, key } = unknownDependencyContext;
47+
48+
let message = `\nNest can't resolve dependencies of the ${type.toString()}`;
4849

4950
if (isNil(index)) {
50-
message += `. Please make sure that the "${key.toString()}" property is available in the current context.`;
51+
message += `.\nPlease make sure that the "${key.toString()}" property is available in the current context.`;
5152
return message;
5253
}
5354
const dependenciesName = (dependencies || []).map(getDependencyName);
5455
dependenciesName[index] = '?';
5556

5657
message += ` (`;
5758
message += dependenciesName.join(', ');
58-
message += `). Please make sure that the argument at index [${index}] is available in the ${getModuleName(
59+
message += `). \nPlease make sure that the argument ${name} at index [${index}] is available in the ${getModuleName(
5960
module,
60-
)} context.`;
61+
)} context.
62+
63+
Potential solutions:
64+
- If ${name} is a provider, is it part of the current ${getModuleName(module) || 'Module'}?
65+
- If ${name} is exported from a separate @Module, is that module imported within ${getModuleName(module) || 'Module'}?
66+
@Module({
67+
imports: [ /* the Module containing ${name} */ ]
68+
})
69+
`;
70+
6171
return message;
6272
};
6373

@@ -70,13 +80,27 @@ export const INVALID_MODULE_MESSAGE = (
7080
text: TemplateStringsArray,
7181
scope: string,
7282
) =>
73-
`Nest cannot create the module instance. Often, this is because of a circular dependency between modules. Use forwardRef() to avoid it. (Read more: https://docs.nestjs.com/fundamentals/circular-dependency.) Scope [${scope}]`;
83+
`
84+
Nest cannot create the module instance.
85+
Often, this is because of a circular dependency between modules.
86+
Use forwardRef() to avoid it.
87+
88+
(Read more: https://docs.nestjs.com/fundamentals/circular-dependency.)
89+
Scope [${scope}]
90+
`;
7491

7592
export const UNKNOWN_EXPORT_MESSAGE = (
76-
text: TemplateStringsArray,
93+
token: string,
7794
module: string,
78-
) =>
79-
`Nest cannot export a provider/module that is not a part of the currently processed module (${module}). Please verify whether each exported unit is available in this particular context.`;
95+
) => {
96+
return `
97+
Nest cannot export a provider/module that is not a part of the currently processed module (${module}).
98+
Please verify whether the exported "${token}" is available in this particular context.
99+
100+
Possible Solutions:
101+
- Is "${token}" part of the relevant providers/imports within ${module}?
102+
`;
103+
};
80104

81105
export const INVALID_CLASS_MESSAGE = (text: TemplateStringsArray, value: any) =>
82106
`ModuleRef cannot instantiate class (${value} is not constructable).`;

packages/core/injector/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export class Module {
391391

392392
if (!importsNames.includes(token as any)) {
393393
const { name } = this.metatype;
394-
throw new UnknownExportException(name);
394+
throw new UnknownExportException(token as any, name);
395395
}
396396
return token;
397397
}

0 commit comments

Comments
 (0)