8 "github.com/golang/protobuf/proto"
9 "github.com/golang/protobuf/ptypes/any"
10 "google.golang.org/grpc/codes"
11 "google.golang.org/grpc/grpclog"
12 "google.golang.org/grpc/status"
15 // HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status.
16 // See: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
17 func HTTPStatusFromCode(code codes.Code) int {
22 return http.StatusRequestTimeout
24 return http.StatusInternalServerError
25 case codes.InvalidArgument:
26 return http.StatusBadRequest
27 case codes.DeadlineExceeded:
28 return http.StatusGatewayTimeout
30 return http.StatusNotFound
31 case codes.AlreadyExists:
32 return http.StatusConflict
33 case codes.PermissionDenied:
34 return http.StatusForbidden
35 case codes.Unauthenticated:
36 return http.StatusUnauthorized
37 case codes.ResourceExhausted:
38 return http.StatusTooManyRequests
39 case codes.FailedPrecondition:
40 return http.StatusPreconditionFailed
42 return http.StatusConflict
43 case codes.OutOfRange:
44 return http.StatusBadRequest
45 case codes.Unimplemented:
46 return http.StatusNotImplemented
48 return http.StatusInternalServerError
49 case codes.Unavailable:
50 return http.StatusServiceUnavailable
52 return http.StatusInternalServerError
55 grpclog.Infof("Unknown gRPC error code: %v", code)
56 return http.StatusInternalServerError
60 // HTTPError replies to the request with the error.
61 // You can set a custom function to this variable to customize error format.
62 HTTPError = DefaultHTTPError
63 // OtherErrorHandler handles the following error used by the gateway: StatusMethodNotAllowed StatusNotFound and StatusBadRequest
64 OtherErrorHandler = DefaultOtherErrorHandler
67 type errorBody struct {
68 Error string `protobuf:"bytes,1,name=error" json:"error"`
69 // This is to make the error more compatible with users that expect errors to be Status objects:
70 // https://github.com/grpc/grpc/blob/master/src/proto/grpc/status/status.proto
71 // It should be the exact same message as the Error field.
72 Message string `protobuf:"bytes,1,name=message" json:"message"`
73 Code int32 `protobuf:"varint,2,name=code" json:"code"`
74 Details []*any.Any `protobuf:"bytes,3,rep,name=details" json:"details,omitempty"`
77 // Make this also conform to proto.Message for builtin JSONPb Marshaler
78 func (e *errorBody) Reset() { *e = errorBody{} }
79 func (e *errorBody) String() string { return proto.CompactTextString(e) }
80 func (*errorBody) ProtoMessage() {}
82 // DefaultHTTPError is the default implementation of HTTPError.
83 // If "err" is an error from gRPC system, the function replies with the status code mapped by HTTPStatusFromCode.
84 // If otherwise, it replies with http.StatusInternalServerError.
86 // The response body returned by this function is a JSON object,
87 // which contains a member whose key is "error" and whose value is err.Error().
88 func DefaultHTTPError(ctx context.Context, mux *ServeMux, marshaler Marshaler, w http.ResponseWriter, _ *http.Request, err error) {
89 const fallback = `{"error": "failed to marshal error message"}`
91 s, ok := status.FromError(err)
93 s = status.New(codes.Unknown, err.Error())
96 w.Header().Del("Trailer")
98 contentType := marshaler.ContentType()
99 // Check marshaler on run time in order to keep backwards compatability
100 // An interface param needs to be added to the ContentType() function on
101 // the Marshal interface to be able to remove this check
102 if httpBodyMarshaler, ok := marshaler.(*HTTPBodyMarshaler); ok {
104 contentType = httpBodyMarshaler.ContentTypeFromMessage(pb)
106 w.Header().Set("Content-Type", contentType)
110 Message: s.Message(),
111 Code: int32(s.Code()),
112 Details: s.Proto().GetDetails(),
115 buf, merr := marshaler.Marshal(body)
117 grpclog.Infof("Failed to marshal error message %q: %v", body, merr)
118 w.WriteHeader(http.StatusInternalServerError)
119 if _, err := io.WriteString(w, fallback); err != nil {
120 grpclog.Infof("Failed to write response: %v", err)
125 md, ok := ServerMetadataFromContext(ctx)
127 grpclog.Infof("Failed to extract ServerMetadata from context")
130 handleForwardResponseServerMetadata(w, mux, md)
131 handleForwardResponseTrailerHeader(w, md)
132 st := HTTPStatusFromCode(s.Code())
134 if _, err := w.Write(buf); err != nil {
135 grpclog.Infof("Failed to write response: %v", err)
138 handleForwardResponseTrailer(w, md)
141 // DefaultOtherErrorHandler is the default implementation of OtherErrorHandler.
142 // It simply writes a string representation of the given error into "w".
143 func DefaultOtherErrorHandler(w http.ResponseWriter, _ *http.Request, msg string, code int) {
144 http.Error(w, msg, code)