Debian Codegraph @

vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/proto_errors.go

raw
1 package runtime
2
3 import (
4 "io"
5 "net/http"
6
7 "golang.org/x/net/context"
8 "google.golang.org/grpc/codes"
9 "google.golang.org/grpc/grpclog"
10 "google.golang.org/grpc/status"
11 )
12
13 // ProtoErrorHandlerFunc handles the error as a gRPC error generated via status package and replies to the request.
14 type ProtoErrorHandlerFunc func(context.Context, *ServeMux, Marshaler, http.ResponseWriter, *http.Request, error)
15
16 var _ ProtoErrorHandlerFunc = DefaultHTTPProtoErrorHandler
17
18 // DefaultHTTPProtoErrorHandler is an implementation of HTTPError.
19 // If "err" is an error from gRPC system, the function replies with the status code mapped by HTTPStatusFromCode.
20 // If otherwise, it replies with http.StatusInternalServerError.
21 //
22 // The response body returned by this function is a Status message marshaled by a Marshaler.
23 //
24 // Do not set this function to HTTPError variable directly, use WithProtoErrorHandler option instead.
25 func DefaultHTTPProtoErrorHandler(ctx context.Context, mux *ServeMux, marshaler Marshaler, w http.ResponseWriter, _ *http.Request, err error) {
26 // return Internal when Marshal failed
27 const fallback = `{"code": 13, "message": "failed to marshal error message"}`
28
29 w.Header().Del("Trailer")
30 w.Header().Set("Content-Type", marshaler.ContentType())
31
32 s, ok := status.FromError(err)
33 if !ok {
34 s = status.New(codes.Unknown, err.Error())
35 }
36
37 buf, merr := marshaler.Marshal(s.Proto())
38 if merr != nil {
39 grpclog.Printf("Failed to marshal error message %q: %v", s.Proto(), merr)
40 w.WriteHeader(http.StatusInternalServerError)
41 if _, err := io.WriteString(w, fallback); err != nil {
42 grpclog.Printf("Failed to write response: %v", err)
43 }
44 return
45 }
46
47 md, ok := ServerMetadataFromContext(ctx)
48 if !ok {
49 grpclog.Printf("Failed to extract ServerMetadata from context")
50 }
51
52 handleForwardResponseServerMetadata(w, mux, md)
53 handleForwardResponseTrailerHeader(w, md)
54 st := HTTPStatusFromCode(s.Code())
55 w.WriteHeader(st)
56 if _, err := w.Write(buf); err != nil {
57 grpclog.Printf("Failed to write response: %v", err)
58 }
59
60 handleForwardResponseTrailer(w, md)
61 }