Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping gRPC error codes to HTTP error codes

My web-application makes HTTP requests to an Extensible Service Proxy (ESP), which in-turn, delegates to a gRPC server (written in Python). Ignoring Android and iOS clients, the architecture is:

enter image description here

The ESP is a nginx reverse proxy.

The gRPC server ("Your code" in the reference architecture) may raise an exception, in which case I use context.abort to raise an exception and terminate the RPC with a non-OK status:

try:
  # Do something that could fail.
except ValueError as e:
  context.abort(grpc.StatusCode.DATA_LOSS, str(e))

While it is possible to use set_code and set_details, they still result in a HTTP status of 200 OK.

There are two problems:

  1. The gRPC status codes are translated by the ESP container (nginx proxy) to a a generic 500 Internal Server Error.

  2. The accompanying details are stripped-out.

  3. and 2. combined means the web client has, at most, a 500 Internal Server Error for all exceptions raised by the gRPC server.

Ultimately, I don't understand how more informative (ideally, custom) errors can be returned to web-clients.

like image 978
Jack Avatar asked Oct 28 '25 13:10

Jack


1 Answers

grpc Status code::DATA_LOSS, are translated to HTTP code 500. The code is here

The grpc status detail (status code and error message) are send back in the response body in JSON format. The code is here

like image 118
Wayne Zhang Avatar answered Oct 30 '25 04:10

Wayne Zhang