blob: b39f55ea824e6cc784890ddf52883953be80499f [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001package runtime
2
3import (
4 "context"
5 "io"
6 "net/http"
7
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"
13)
14
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
17func HTTPStatusFromCode(code codes.Code) int {
18 switch code {
19 case codes.OK:
20 return http.StatusOK
21 case codes.Canceled:
22 return http.StatusRequestTimeout
23 case codes.Unknown:
24 return http.StatusInternalServerError
25 case codes.InvalidArgument:
26 return http.StatusBadRequest
27 case codes.DeadlineExceeded:
28 return http.StatusGatewayTimeout
29 case codes.NotFound:
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
41 case codes.Aborted:
42 return http.StatusConflict
43 case codes.OutOfRange:
44 return http.StatusBadRequest
45 case codes.Unimplemented:
46 return http.StatusNotImplemented
47 case codes.Internal:
48 return http.StatusInternalServerError
49 case codes.Unavailable:
50 return http.StatusServiceUnavailable
51 case codes.DataLoss:
52 return http.StatusInternalServerError
53 }
54
55 grpclog.Infof("Unknown gRPC error code: %v", code)
56 return http.StatusInternalServerError
57}
58
59var (
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
65)
66
67type 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"`
75}
76
77// Make this also conform to proto.Message for builtin JSONPb Marshaler
78func (e *errorBody) Reset() { *e = errorBody{} }
79func (e *errorBody) String() string { return proto.CompactTextString(e) }
80func (*errorBody) ProtoMessage() {}
81
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.
85//
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().
88func DefaultHTTPError(ctx context.Context, mux *ServeMux, marshaler Marshaler, w http.ResponseWriter, _ *http.Request, err error) {
89 const fallback = `{"error": "failed to marshal error message"}`
90
91 w.Header().Del("Trailer")
92 w.Header().Set("Content-Type", marshaler.ContentType())
93
94 s, ok := status.FromError(err)
95 if !ok {
96 s = status.New(codes.Unknown, err.Error())
97 }
98
99 body := &errorBody{
100 Error: s.Message(),
101 Message: s.Message(),
102 Code: int32(s.Code()),
103 Details: s.Proto().GetDetails(),
104 }
105
106 buf, merr := marshaler.Marshal(body)
107 if merr != nil {
108 grpclog.Infof("Failed to marshal error message %q: %v", body, merr)
109 w.WriteHeader(http.StatusInternalServerError)
110 if _, err := io.WriteString(w, fallback); err != nil {
111 grpclog.Infof("Failed to write response: %v", err)
112 }
113 return
114 }
115
116 md, ok := ServerMetadataFromContext(ctx)
117 if !ok {
118 grpclog.Infof("Failed to extract ServerMetadata from context")
119 }
120
121 handleForwardResponseServerMetadata(w, mux, md)
122 handleForwardResponseTrailerHeader(w, md)
123 st := HTTPStatusFromCode(s.Code())
124 w.WriteHeader(st)
125 if _, err := w.Write(buf); err != nil {
126 grpclog.Infof("Failed to write response: %v", err)
127 }
128
129 handleForwardResponseTrailer(w, md)
130}
131
132// DefaultOtherErrorHandler is the default implementation of OtherErrorHandler.
133// It simply writes a string representation of the given error into "w".
134func DefaultOtherErrorHandler(w http.ResponseWriter, _ *http.Request, msg string, code int) {
135 http.Error(w, msg, code)
136}