Skip to content

Commit

Permalink
Merge pull request #1746 from micalevisk/patch-1
Browse files Browse the repository at this point in the history
feat: less verbose module referencing on errors/debug messages
  • Loading branch information
kamilmysliwiec authored Jul 1, 2024
2 parents 112b556 + 0f0176a commit ea180e4
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/conditional.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { DynamicModule, Logger, ModuleMetadata } from '@nestjs/common';
import { DynamicModule, ForwardReference, Logger, ModuleMetadata, Type } from '@nestjs/common';
import { ConfigModule } from './config.module';

/**
* Same logic as in `@nestjs/core` package.
* @param instance The instance which should get the name from
* @returns The name of an instance or `undefined`
*/
const getInstanceName = (instance: unknown): string | undefined => {
if ((instance as ForwardReference)?.forwardRef) {
return (instance as ForwardReference).forwardRef()?.name;
}

if ((instance as DynamicModule).module) {
return (instance as DynamicModule).module?.name;
}

return (instance as Type).name;
};

/**
* @publicApi
*/
Expand All @@ -14,10 +31,11 @@ export class ConditionalModule {
options?: { timeout?: number; debug?: boolean },
) {
const { timeout = 5000, debug = true } = options ?? {};
const moduleName = getInstanceName(module) || module.toString();

const timer = setTimeout(() => {
throw new Error(
`Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${module.toString()} should be registered or not`,
`Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${moduleName} should be registered or not`,
);
}, timeout);
timer.unref();
Expand All @@ -40,7 +58,7 @@ export class ConditionalModule {
} else {
if (debug) {
Logger.debug(
`${condition.toString()} evaluated to false. Skipping the registration of ${module.toString()}`,
`${condition.toString()} evaluated to false. Skipping the registration of ${moduleName}`,
ConditionalModule.name,
);
}
Expand Down

0 comments on commit ea180e4

Please sign in to comment.