nadavvadan
nadavvadan

Reputation: 4190

gRPC error returns with unexpected details field

I'm implementing gRPC error handling in my C++ server, which has routes called by another, nodejs-based, server (which is actually the client in this case).

My problem is that the error_details param passed to the grpc::Status constructor on the C++ side is not the same as the error_details the client receives.

C++ server:

return grpc::Status(
    isError ? grpc::StatusCode::UNKNOWN : grpc::StatusCode::OK,
    "application-specific error code", // the error_message param
    "Extended error details"           // the error_details param
);

NodeJS client:

try {
  await grpc.makeCall({params: 12345})
} catch(err) {
  const { details, message } = err
  console.log({ details, message }); 
  // -> { details: "application-specific error code", message: "2 UNKNOWN application-specific error code" }
  // Expected message:
  // -> { details: "Extended error", message: "application-specific error code" }
}

to clarify, the details field in the client contains the message field from the server, while the message field in the client contains the message field from the server plus a gRPC status code as a prefix.

I did note, however, that the details field I'm after, is available under this (incredibly straightforward) prop:

err.metadata._internal_repr['grpc-status-details-bin'][0].toString() // Originally a buffer  

So my question is:
What the heck? am I missing something?

Upvotes: 1

Views: 2632

Answers (1)

murgatroid99
murgatroid99

Reputation: 20297

The different APIs here have different representations of the same underlying protocol information, and they happen to use names that collide. For reference, see the protocol specification. In that C++ API, the error_message parameter corresponds to the "Status-Message" part of the response, and the error_details parameter corresponds to the grpc-status-details-bin custom metadata item. As stated in this comment the intention is that the error_details parameter will contain a serialized google.rpc.Status proto message.

In the Node API, the details value corresponds to the "Status-Message" part of the response (which, again, was the error_message parameter in C++). The message value is just an aggregate user-readable string that combines the status code, the human-readable name of the status code, and the details string. As mentioned in the question, the grpc-status-details-bin metadata item does not have a special accessor, it's just in the metadata value.

Upvotes: 1

Related Questions