Search code examples
springspring-bootgrpcgrpc-javaenvoyproxy

How to send error response as json in Grpc using envoy-proxy


I am throwing error from grpc service using resonseObserver.onError() but I am not getting messages in json format while hitting REST API from rest client, though the positive scenario is working fine and giving response as json.

I am using envoy as a transcoder, can anyone help me with how to get error response also as json. Currently I am getting BadRequest on error scenarios. The project is in SpringBoot.

TIA


Solution

  • You can use convert_grpc_status: true for do this.

          http_filters:
          - name: envoy.filters.http.grpc_json_transcoder
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.grpc_json_transcoder.v3.GrpcJsonTranscoder
              proto_descriptor: "/tmp/envoy/proto.pb"
              services: ["xxxxxxxx"]
              convert_grpc_status: true
              print_options:
                always_print_primitive_fields: true
                always_print_enums_as_ints: false
                preserve_proto_field_names: false
    

    If you mean return details key like this:

    {
      "code": 3,
      "message": "API call quota depleted",
      "details": [
        {
          "@type": "type.googleapis.com/google.rpc.ResourceInfo",
          "resourceType": "xxxxxx",
          "resourceName": "",
          "owner": "",
          "description": ""
        }
      ]
    }
    

    You MUST compile you .proto file with:

    import "google/rpc/error_details.proto";
    

    because Envoy can't deserialize binary details from your backend server without error types.

    Also you can read how you can send detailed error response with Python: How to send error details like as BadRequest