Skip to content

Commit

Permalink
Add request in error handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Low committed Jan 8, 2016
1 parent f647940 commit b0703d4
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 50 deletions.
22 changes: 11 additions & 11 deletions examples/examplepb/a_bit_of_everything.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/examplepb/echo_service.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 26 additions & 26 deletions examples/examplepb/flow_combination.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
8 changes: 7 additions & 1 deletion runtime/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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)
}
Loading

0 comments on commit b0703d4

Please sign in to comment.