|
9 | 9 | "google.golang.org/grpc/credentials" |
10 | 10 | "google.golang.org/grpc/credentials/insecure" |
11 | 11 | "google.golang.org/grpc/keepalive" |
| 12 | + "google.golang.org/grpc/status" |
12 | 13 | "google.golang.org/protobuf/encoding/protojson" |
13 | 14 | "net" |
14 | 15 | "net/http" |
@@ -148,13 +149,25 @@ func (t *RpcServer) Serve() error { |
148 | 149 | DiscardUnknown: true, |
149 | 150 | } |
150 | 151 | } |
151 | | - mux := runtime.NewServeMux( |
| 152 | + muxOpts := []runtime.ServeMuxOption{ |
152 | 153 | // Format for using proto names in json https://grpc-ecosystem.github.io/grpc-gateway/docs/mapping/customizing_your_gateway/#using-proto-names-in-json |
153 | 154 | runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{ |
154 | 155 | MarshalOptions: *marshalOptions, |
155 | 156 | UnmarshalOptions: *unmarshalOptions, |
156 | 157 | }), |
157 | | - ) |
| 158 | + } |
| 159 | + if t.Logger != nil { |
| 160 | + customHTTPError := func(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) { |
| 161 | + defer runtime.DefaultHTTPErrorHandler(ctx, mux, marshaler, w, r, err) |
| 162 | + if err == nil { |
| 163 | + return |
| 164 | + } |
| 165 | + st := status.Convert(err) |
| 166 | + t.Logger.Log(ctx, logging.LevelInfo, "gateway bad request", st.Code(), st.Message(), st.Details()) |
| 167 | + } |
| 168 | + muxOpts = append(muxOpts, runtime.WithErrorHandler(customHTTPError)) |
| 169 | + } |
| 170 | + mux := runtime.NewServeMux(muxOpts...) |
158 | 171 | t.Gateway.Registrar(mux, conn) |
159 | 172 | gateway := &http.Server{ |
160 | 173 | Addr: t.Gateway.Addr, |
|
0 commit comments