[英]NestJS HttpService All Method API Call is not working when Interceptor is used
[英]Binding interceptor to NestJS microservice method
我刚刚创建了一个简单的拦截器来覆盖我的应用程序抛出的每个错误,就像 Nest 文档中的那个:
@Injectable()
export class ErrorsInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next
.handle()
.pipe(
catchError(err => throwError(() => new ApplicationException())),
);
}
}
尽管由 http 请求引起的异常确实被该拦截器捕获,但我无法使其与 RPC 请求(如 KafjaJS 和事件)一起工作。 就像文档一样,我将它绑定在我的app.module
上:
{
provide: APP_INTERCEPTOR,
useClass: ErrorsInterceptor,
}
我知道我可能遗漏了一些东西,有人可以澄清我在哪里以及为什么我正在做的事情不起作用以及如何使它起作用吗?
@Edit:我忘了提到我让它在我的控制器方法上方工作@UseInterceptors(),但我想让它在没有它的情况下工作。
@Edit 2:我有一个混合应用程序,这就是我的主要外观(如杰伊所问):
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
logger: WinstonModule.createLogger(winstonTransports),
});
app.connectMicroservice<MicroserviceOptions>(kafkaConfig);
const logger = app.get<winston.Logger>(WINSTON_MODULE_NEST_PROVIDER);
app.useLogger(logger);
app.enableCors(corsConfig);
await app.startAllMicroservices();
await app.listen(env.PORT);
}
使用混合应用程序时,您需要将{ inheritAppConfig: true }
添加到connectMicroservice()
方法作为第二个参数,如文档中所述。 这意味着您的main.ts
应该是
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
logger: WinstonModule.createLogger(winstonTransports),
});
app.connectMicroservice<MicroserviceOptions>(kafkaConfig, { inheritAppConfig: true });
const logger = app.get<winston.Logger>(WINSTON_MODULE_NEST_PROVIDER);
app.useLogger(logger);
app.enableCors(corsConfig);
await app.startAllMicroservices();
await app.listen(env.PORT);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.