Skip to content
文档
Error

Error

错误处理是保证服务正常运行的重要工作。gRPCity 也提供方便的错误处理方式。一般情况下,建议使用try/catch进行错误捕捉,采用throw进行错误暴露。

客户端发现错误

客户端调用服务端,如果服务端报错,则会返回ERROR对象,对象包含下面4个字段:

  • name: 值为GrpcClientError, 可以用来判断是否为客户端 rpc 的错误;
  • code: gRPC Status Code;
  • message: gRPC Server emit message details;
  • stack: file error trace stack;

ERROR对象完全展开后如下所示:

{
    "name":"GrpcClientError",
    "code":2,
    "message":"/test.helloworld.Greeter/SayHello (2 UNKNOWN: Error: throw error because x-throw-error [Error Message From Server, stack: at Greeter.SayHello (/Users/lau/Documents/GitHub/grpcity/test/server.js:27:13)])",
    "stack": "
        at start (/Users/lau/Documents/GitHub/grpcity/test/client.js:36:72)
        ...
        at callErrorFromStatus (/Users/lau/Documents/GitHub/grpcity/node_modules/@grpc/grpc-js/build/src/call.js:31:19)
        at Object.onReceiveStatus (/Users/lau/Documents/GitHub/grpcity/node_modules/@grpc/grpc-js/build/src/client.js:192:76)
    "
}

服务端触发错误

下面展示如果客户端发送x-throw-error元信息,服务端执行sayHello()检查该元信息后主动触发报错的例子。

async sayHello (call) {
    const metadata = call.metadata.clone()
    metadata.add('x-timestamp-server', 'received=' + new Date().toISOString())
    call.sendMetadata(metadata)
 
    if (metadata.get('x-throw-error').length > 0) {
        throw new Error('throw error because x-throw-error')
    }
 
    return { message: `hello, ${call.request.name || 'world'}` }
}

服务端主动触发报错后,客户端捕捉到的错误报错,如上面客户端发现错误展示的例子一样。