I use "nest-cls" to integrate my NestJS application with async local storage
I configured nestjs-cls with;
export const setupAsyncStorageFactory = (
configService: ConfigService<ConfigurationInterface, true>,
): ClsModuleOptions => ({
global: true,
interceptor: {
mount: true,
generateId: true,
idGenerator: (context: ExecutionContext) => {
const request = context.switchToHttp().getRequest<Request>();
const nodeEnv = configService.get("nodeEnv", { infer: true });
const isCloudEnv = nodeEnv === "production" || nodeEnv === "staging";
return isCloudEnv ? request.header("x-request-id") ?? "no-id" : randomUUID();
},
},
});
Then I use this function in my "app.module.ts" file:
ClsModule.forRootAsync({
global: true,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: setupAsyncStorageFactory,
}),
I try to resolve the ClsService within a global exception filter I configured in my "app.module.ts":
providers: [
{
provide: APP_FILTER,
useClass: AllExceptionsFilter,
scope: Scope.REQUEST,
},
And the filter itself:
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
constructor(
private readonly httpAdapterHost: HttpAdapterHost,
private readonly clsService: ClsService<AsyncStorageStore>
) {}
public catch(exception: unknown, host: ArgumentsHost) {
console.log(this.clsService.getId());
}
}
But I get undefined instead. I have a global interceptor declared in the same manner (with APP_FILTER in "app.module.ts" file) where I do succeed to get the request ID. So why not in exception filter?