2

Remote Server

@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'
        })
} 

Client code

@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;
    }
  }

Proto defination.

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;
}

If error comes it is not going to catch block, just showing the server error. I want to catch the error if remote throwing any error.

AMit SiNgh
  • 325
  • 4
  • 17

1 Answers1

0

Try this one:

@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

Tyler2P
  • 2,324
  • 26
  • 22
  • 31