繁体   English   中英

NestJS - 微服务 - Kafka 异常处理

[英]NestJS - Microservices - Kafka Exception Handling

我正在使用 NestJS 来实现微服务架构,我正在使用使用 NestJS 的 CLIENT 应用程序,

客户端应用程序接收到 rest 请求并发送到 Kafka 以获取结果

  try {
     const pattern = 'findDoctors';
     const payload = body;
     const doctors = await lastValueFrom(
       this.client.send(pattern, payload).pipe(timeout(50000)),
     );
     return doctors;
   } catch (e) {
     console.log(e);
     throw new Error(e);
   }

而在微服务端(暂时是混合应用,稍后会移除rest apis)

 @MessagePattern('findDoctors')
 findDoctors(@Payload() message): any {
   return this.doctorService.searchForDoctors(message.value);
 }
async searchForDoctors(data): Promise<any[]> {
   this.logger.info('Started search for doctors job');
   throw 'Not Found';
}

在我抛出异常后,我进入日志

node:17720) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object
   at Function.from (buffer.js:330:9)
   at ServerKafka.assignErrorHeader (C:\Users\Desktop\Projects\node_modules\@nestjs\microservices\server\server-kafka.js:137:73)
   at ServerKafka.sendMessage (C:\Users\Desktop\Projects\node_modules\@nestjs\microservices\server\server-kafka.js:119:14)
   at C:\Users\Desktop\Projects\node_modules\@nestjs\microservices\server\server-kafka.js:81:31
   at C:\Users\Desktop\node_modules\@nestjs\microservices\server\server.js:46:31
   at processTicksAndRejections (internal/process/task_queues.js:79:11)
(node:17720) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:17720) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

并且客户端只是等待超时并且永远不会收到来自微服务的异常响应

尝试使用 RpcException 但它是一样的

找到了答案

用过的

return new RpcException('not found');

代替

throw new RpcException('not found')

抛出异常需要异常过滤器来捕获它

@Catch(RpcException)
export class ExceptionFilter implements RpcExceptionFilter<RpcException> {
  catch(exception: RpcException, host: ArgumentsHost): Observable<any> {
    return throwError(exception.getError());  
  }
}

在客户端,您可以在使用过滤器时捕获错误并返回正常的 http 异常,或者也使用过滤器

 @Post('/search')
  async findAll(@Body() body) {
    console.log('Sending kafka msg');
    try {
      const doctors = await this.doctorService.findDoctors(body);
      return doctors;
    } catch (e) {
      console.log(e)
      throw new HttpException({
        status: '500',
        error: e.message,
      }, 500)
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM