blob: 41d54ef916aafcaba75779bbaefaa8ec37403956 [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001package 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 s, ok := status.FromError(err)
92 if !ok {
93 s = status.New(codes.Unknown, err.Error())
94 }
95
96 w.Header().Del("Trailer")
97
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 {
103 pb := s.Proto()
104 contentType = httpBodyMarshaler.ContentTypeFromMessage(pb)
105 }
106 w.Header().Set("Content-Type", contentType)
107
108 body := &errorBody{
109 Error: s.Message(),
110 Message: s.Message(),
111 Code: int32(s.Code()),
112 Details: s.Proto().GetDetails(),
113 }
114
115 buf, merr := marshaler.Marshal(body)
116 if merr != nil {
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)
121 }
122 return
123 }
124
125 md, ok := ServerMetadataFromContext(ctx)
126 if !ok {
127 grpclog.Infof("Failed to extract ServerMetadata from context")
128 }
129
130 handleForwardResponseServerMetadata(w, mux, md)
131 handleForwardResponseTrailerHeader(w, md)
132 st := HTTPStatusFromCode(s.Code())
133 w.WriteHeader(st)
134 if _, err := w.Write(buf); err != nil {
135 grpclog.Infof("Failed to write response: %v", err)
136 }
137
138 handleForwardResponseTrailer(w, md)
139}
140
141// DefaultOtherErrorHandler is the default implementation of OtherErrorHandler.
142// It simply writes a string representation of the given error into "w".
143func DefaultOtherErrorHandler(w http.ResponseWriter, _ *http.Request, msg string, code int) {
144 http.Error(w, msg, code)
145}