【问题标题】:Nestjs | grpc : How to handle remote error from client side巢穴 | grpc:如何处理来自客户端的远程错误
【发布时间】:2022-07-28 13:23:19
【问题描述】:

远程服务器

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

@UseFilters(new RpcExceptionHandler())
@GrpcMethod('AppController', 'Accumulate') 
async accumulate(numberArray: INumberArray, metadata: any): Promise<ISumOfNumberArray> { 

    throw new RpcException({
          code: 5,
          message: 'Data Not Found'
        })
} 

客户端代码

@Get('add')
  async getSumc(@Query('data') data: number[]) {
    try {
      let ata = await this.grpcService.accumulate({ data }); 
      return ata;
    } catch (err) {
      //logic here if error comes
      return err;
    }
  }

原型定义。

syntax = "proto3";

package app;

// Declare a service for each controller you have
service AppController {
  // Declare an rpc for each method that is called via gRPC
  rpc Accumulate (NumberArray) returns (SumOfNumberArray);
}

// Declare the types used above
message NumberArray {
  repeated double data = 1;
}
message SumOfNumberArray {
  double sum = 1;
}

如果出现错误,它不会捕获块,只是显示服务器错误。 如果远程抛出任何错误,我想捕获错误。

【问题讨论】:

  • 没有人敢回答这个问题。 :)

标签: node.js error-handling nestjs grpc


【解决方案1】:

试试这个:

@Get('add')
  async getSumc(@Query('data') data: number[]) {
    try {
      let ata = await this.grpcService.accumulate({ data }).toPromise(); 
      return ata;
    } catch (e) {
      throw new RpcException(e);
    }
  }

Example here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2019-06-03
    • 1970-01-01
    • 2010-11-19
    • 2017-02-12
    • 1970-01-01
    • 2019-06-27
    相关资源
    最近更新 更多