From b0703d482f6f8016c2aec0d552e10ce977839ef3 Mon Sep 17 00:00:00 2001 From: Daniel Low Date: Fri, 8 Jan 2016 17:40:28 +0000 Subject: [PATCH] Add request in error handlers --- .../examplepb/a_bit_of_everything.pb.gw.go | 22 ++++---- examples/examplepb/echo_service.pb.gw.go | 4 +- examples/examplepb/flow_combination.pb.gw.go | 52 +++++++++---------- .../gengateway/template.go | 2 +- runtime/errors.go | 8 ++- runtime/errors_test.go | 3 +- runtime/handler.go | 4 +- runtime/mux.go | 12 ++--- 8 files changed, 57 insertions(+), 50 deletions(-) diff --git a/examples/examplepb/a_bit_of_everything.pb.gw.go b/examples/examplepb/a_bit_of_everything.pb.gw.go index 1383694f037..bbc9f5a6a5e 100644 --- a/examples/examplepb/a_bit_of_everything.pb.gw.go +++ b/examples/examplepb/a_bit_of_everything.pb.gw.go @@ -436,7 +436,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("POST", pattern_ABitOfEverythingService_Create_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_Create_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -447,7 +447,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("POST", pattern_ABitOfEverythingService_CreateBody_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_CreateBody_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -458,7 +458,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("POST", pattern_ABitOfEverythingService_BulkCreate_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_BulkCreate_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -469,7 +469,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("GET", pattern_ABitOfEverythingService_Lookup_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_Lookup_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -480,7 +480,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("GET", pattern_ABitOfEverythingService_List_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_List_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -491,7 +491,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("PUT", pattern_ABitOfEverythingService_Update_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_Update_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -502,7 +502,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("DELETE", pattern_ABitOfEverythingService_Delete_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_Delete_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -513,7 +513,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("GET", pattern_ABitOfEverythingService_Echo_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_Echo_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -524,7 +524,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("POST", pattern_ABitOfEverythingService_Echo_1, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_Echo_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -535,7 +535,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("GET", pattern_ABitOfEverythingService_Echo_2, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_Echo_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -546,7 +546,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se mux.Handle("POST", pattern_ABitOfEverythingService_BulkEcho_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_ABitOfEverythingService_BulkEcho_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } diff --git a/examples/examplepb/echo_service.pb.gw.go b/examples/examplepb/echo_service.pb.gw.go index 13fa7a94d82..5b041546055 100644 --- a/examples/examplepb/echo_service.pb.gw.go +++ b/examples/examplepb/echo_service.pb.gw.go @@ -96,7 +96,7 @@ func RegisterEchoServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn mux.Handle("POST", pattern_EchoService_Echo_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_EchoService_Echo_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -107,7 +107,7 @@ func RegisterEchoServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn mux.Handle("POST", pattern_EchoService_EchoBody_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_EchoService_EchoBody_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } diff --git a/examples/examplepb/flow_combination.pb.gw.go b/examples/examplepb/flow_combination.pb.gw.go index d33becb0eba..9e056d94b97 100644 --- a/examples/examplepb/flow_combination.pb.gw.go +++ b/examples/examplepb/flow_combination.pb.gw.go @@ -816,7 +816,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcEmptyRpc_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcEmptyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -827,7 +827,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcEmptyStream_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcEmptyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -838,7 +838,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_StreamEmptyRpc_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_StreamEmptyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -849,7 +849,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_StreamEmptyStream_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_StreamEmptyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -860,7 +860,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyRpc_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -871,7 +871,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyRpc_1, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyRpc_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -882,7 +882,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyRpc_2, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyRpc_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -893,7 +893,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyRpc_3, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyRpc_3(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -904,7 +904,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyRpc_4, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyRpc_4(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -915,7 +915,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyRpc_5, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyRpc_5(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -926,7 +926,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyRpc_6, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyRpc_6(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -937,7 +937,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcPathSingleNestedRpc_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcPathSingleNestedRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -948,7 +948,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcPathNestedRpc_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcPathNestedRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -959,7 +959,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcPathNestedRpc_1, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcPathNestedRpc_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -970,7 +970,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcPathNestedRpc_2, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcPathNestedRpc_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -981,7 +981,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyStream_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -992,7 +992,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyStream_1, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyStream_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -1003,7 +1003,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyStream_2, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyStream_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -1014,7 +1014,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyStream_3, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyStream_3(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -1025,7 +1025,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyStream_4, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyStream_4(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -1036,7 +1036,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyStream_5, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyStream_5(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -1047,7 +1047,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcBodyStream_6, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcBodyStream_6(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -1058,7 +1058,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcPathSingleNestedStream_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcPathSingleNestedStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -1069,7 +1069,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcPathNestedStream_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcPathNestedStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -1080,7 +1080,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcPathNestedStream_1, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcPathNestedStream_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } @@ -1091,7 +1091,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, mux.Handle("POST", pattern_FlowCombination_RpcPathNestedStream_2, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_FlowCombination_RpcPathNestedStream_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } diff --git a/protoc-gen-grpc-gateway/gengateway/template.go b/protoc-gen-grpc-gateway/gengateway/template.go index cefb0253a8d..f8c4e896518 100644 --- a/protoc-gen-grpc-gateway/gengateway/template.go +++ b/protoc-gen-grpc-gateway/gengateway/template.go @@ -247,7 +247,7 @@ func Register{{$svc.GetName}}Handler(ctx context.Context, mux *runtime.ServeMux, mux.Handle({{$b.HTTPMethod | printf "%q"}}, pattern_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { resp, err := request_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { - runtime.HTTPError(ctx, w, err) + runtime.HTTPError(ctx, w, req, err) return } {{if $m.GetServerStreaming}} diff --git a/runtime/errors.go b/runtime/errors.go index dc38373c8b1..3525e2c6a01 100644 --- a/runtime/errors.go +++ b/runtime/errors.go @@ -58,6 +58,8 @@ var ( // HTTPError replies to the request with the error. // You can set a custom function to this variable to customize error format. HTTPError = DefaultHTTPError + // This handles the following error used by the gateway: StatusMethodNotAllowed StatusNotFound and StatusBadRequest + OtherErrorHandler = DefaultOtherErrorHandler ) type errorBody struct { @@ -70,7 +72,7 @@ type errorBody struct { // // The response body returned by this function is a JSON object, // which contains a member whose key is "error" and whose value is err.Error(). -func DefaultHTTPError(ctx context.Context, w http.ResponseWriter, err error) { +func DefaultHTTPError(ctx context.Context, w http.ResponseWriter, _ *http.Request, err error) { const fallback = `{"error": "failed to marshal error message"}` w.Header().Set("Content-Type", "application/json") @@ -91,3 +93,7 @@ func DefaultHTTPError(ctx context.Context, w http.ResponseWriter, err error) { glog.Errorf("Failed to write response: %v", err) } } + +func DefaultOtherErrorHandler(w http.ResponseWriter, _ *http.Request, error string, code int) { + http.Error(w, error, code) +} diff --git a/runtime/errors_test.go b/runtime/errors_test.go index 5f6edaa57dd..9471c1d76a1 100644 --- a/runtime/errors_test.go +++ b/runtime/errors_test.go @@ -34,7 +34,8 @@ func TestDefaultHTTPError(t *testing.T) { }, } { w := httptest.NewRecorder() - runtime.DefaultHTTPError(ctx, w, spec.err) + req, _ := http.NewRequest("", "", nil) // Pass in an empty request to match the signature + runtime.DefaultHTTPError(ctx, w, req, spec.err) if got, want := w.Header().Get("Content-Type"), "application/json"; got != want { t.Errorf(`w.Header().Get("Content-Type") = %q; want %q; on spec.err=%v`, got, want, spec.err) diff --git a/runtime/handler.go b/runtime/handler.go index ebf1c9906ca..02d7c92c349 100644 --- a/runtime/handler.go +++ b/runtime/handler.go @@ -71,14 +71,14 @@ func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http func ForwardResponseMessage(ctx context.Context, w http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error) { w.Header().Set("Content-Type", "application/json") if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil { - HTTPError(ctx, w, err) + HTTPError(ctx, w, req, err) return } buf, err := json.Marshal(resp) if err != nil { glog.Errorf("Marshal error: %v", err) - HTTPError(ctx, w, err) + HTTPError(ctx, w, req, err) return } diff --git a/runtime/mux.go b/runtime/mux.go index 35962389009..3ab1e4b8118 100644 --- a/runtime/mux.go +++ b/runtime/mux.go @@ -57,7 +57,7 @@ func (s *ServeMux) Handle(meth string, pat Pattern, h HandlerFunc) { func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { path := r.URL.Path if !strings.HasPrefix(path, "/") { - http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + OtherErrorHandler(w, r, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return } @@ -65,7 +65,7 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { l := len(components) var verb string if idx := strings.LastIndex(components[l-1], ":"); idx == 0 { - http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + OtherErrorHandler(w, r, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } else if idx > 0 { c := components[l-1] @@ -75,7 +75,7 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { if override := r.Header.Get("X-HTTP-Method-Override"); override != "" && isPathLengthFallback(r) { r.Method = strings.ToUpper(override) if err := r.ParseForm(); err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + OtherErrorHandler(w, r, err.Error(), http.StatusBadRequest) return } } @@ -103,17 +103,17 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { // X-HTTP-Method-Override is optional. Always allow fallback to POST. if isPathLengthFallback(r) { if err := r.ParseForm(); err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + OtherErrorHandler(w, r, err.Error(), http.StatusBadRequest) return } h.h(w, r, pathParams) return } - http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) + OtherErrorHandler(w, r, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } } - http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + OtherErrorHandler(w, r, http.StatusText(http.StatusNotFound), http.StatusNotFound) } // GetForwardResponseOptions returns the ForwardResponseOptions associated with this ServeMux.