Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support streaming API calls #1

Merged
merged 8 commits into from
Apr 8, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Map grpc error codes to http response status codes
  • Loading branch information
yugui committed Apr 7, 2015
commit a502a9f3b4b4dfe07e11bef29f53aa6cf8303494
32 changes: 16 additions & 16 deletions examples/a_bit_of_everything.pb.gw.go

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

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

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

6 changes: 3 additions & 3 deletions protoc-gen-grpc-gateway/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ type {{.ServiceName}}_{{.Name}}StreamChunk struct {
func handle_{{.ServiceName}}_{{.Name}}(ctx context.Context, c web.C, client {{.ServiceName}}Client, w http.ResponseWriter, req *http.Request) {
stream, err := request_{{.ServiceName}}_{{.Name}}(ctx, c, client, req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
runtime.HTTPError(w, err)
return
}

Expand Down Expand Up @@ -463,13 +463,13 @@ func handle_{{.ServiceName}}_{{.Name}}(ctx context.Context, c web.C, client {{.S
func handle_{{.ServiceName}}_{{.Name}}(ctx context.Context, c web.C, client {{.ServiceName}}Client, w http.ResponseWriter, req *http.Request) {
resp, err := request_{{.ServiceName}}_{{.Name}}(ctx, c, client, req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
runtime.HTTPError(w, err)
return
}
buf, err := json.Marshal(resp)
if err != nil {
glog.Errorf("Marshal error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
runtime.HTTPError(w, err)
return
}

Expand Down
8 changes: 4 additions & 4 deletions protoc-gen-grpc-gateway/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ func request_EchoService_Echo(ctx context.Context, c web.C, client EchoServiceCl
func handle_EchoService_Echo(ctx context.Context, c web.C, client EchoServiceClient, w http.ResponseWriter, req *http.Request) {
resp, err := request_EchoService_Echo(ctx, c, client, req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
runtime.HTTPError(w, err)
return
}
buf, err := json.Marshal(resp)
if err != nil {
glog.Errorf("Marshal error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
runtime.HTTPError(w, err)
return
}

Expand All @@ -167,13 +167,13 @@ func request_EchoService_EchoBody(ctx context.Context, c web.C, client EchoServi
func handle_EchoService_EchoBody(ctx context.Context, c web.C, client EchoServiceClient, w http.ResponseWriter, req *http.Request) {
resp, err := request_EchoService_EchoBody(ctx, c, client, req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
runtime.HTTPError(w, err)
return
}
buf, err := json.Marshal(resp)
if err != nil {
glog.Errorf("Marshal error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
runtime.HTTPError(w, err)
return
}

Expand Down
60 changes: 60 additions & 0 deletions runtime/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package runtime

import (
"net/http"

"github.com/golang/glog"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)

// HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status.
func HTTPStatusFromCode(code codes.Code) int {
switch code {
case codes.OK:
return http.StatusOK
case codes.Canceled:
return http.StatusRequestTimeout
case codes.Unknown:
return http.StatusInternalServerError
case codes.InvalidArgument:
return http.StatusBadRequest
case codes.DeadlineExceeded:
return http.StatusRequestTimeout
case codes.NotFound:
return http.StatusNotFound
case codes.AlreadyExists:
return http.StatusConflict
case codes.PermissionDenied:
return http.StatusForbidden
case codes.Unauthenticated:
return http.StatusUnauthorized
case codes.ResourceExhausted:
return http.StatusForbidden
case codes.FailedPrecondition:
return http.StatusPreconditionFailed
case codes.Aborted:
return http.StatusConflict
case codes.OutOfRange:
return http.StatusBadRequest
case codes.Unimplemented:
return http.StatusNotImplemented
case codes.Internal:
return http.StatusInternalServerError
case codes.Unavailable:
return http.StatusServiceUnavailable
case codes.DataLoss:
return http.StatusInternalServerError
}

glog.Errorf("Unknown gRPC error code: %v", code)
return http.StatusInternalServerError
}

// HTTPError replies to the request with the error.
// If "err" is an error from gRPC system, the function replies with the status code mapped by HTTPStatusFromCode.
// If otherwise, it replies with http.StatusInternalServerError.
func HTTPError(w http.ResponseWriter, err error) {
st := HTTPStatusFromCode(grpc.Code(err))
http.Error(w, err.Error(), st)
}