From 7bc97143b1beb3e2a0272dafd67a8f9823d6cc7a Mon Sep 17 00:00:00 2001 From: xiaozhang Date: Mon, 5 Jun 2017 17:55:37 +0800 Subject: [PATCH] ISSUE#405: customize the error return --- examples/clients/echo/EchoServiceApi.go | 61 ++++++++++++++ .../clients/echo/ExamplepbSimpleMessage.go | 1 + .../examplepb/a_bit_of_everything.pb.gw.go | 42 +++++----- examples/examplepb/echo_service.pb.go | 32 +++++--- examples/examplepb/echo_service.pb.gw.go | 81 ++++++++++++++++++- examples/examplepb/echo_service.proto | 4 + examples/examplepb/echo_service.swagger.json | 37 +++++++++ examples/examplepb/flow_combination.pb.gw.go | 48 +++++------ examples/integration_test.go | 20 +++++ .../gengateway/template.go | 2 +- 10 files changed, 270 insertions(+), 58 deletions(-) diff --git a/examples/clients/echo/EchoServiceApi.go b/examples/clients/echo/EchoServiceApi.go index 0cf5b582740..fe6d7341d3d 100644 --- a/examples/clients/echo/EchoServiceApi.go +++ b/examples/clients/echo/EchoServiceApi.go @@ -143,3 +143,64 @@ func (a EchoServiceApi) EchoBody (body ExamplepbSimpleMessage) (ExamplepbSimpleM return *successPayload, err } +/** + * Echo method receives a simple message and returns it. + * The message posted as the id parameter will also be\nreturned. + * @param id + * @param num + * @return ExamplepbSimpleMessage + */ +//func (a EchoServiceApi) Echo_1 (id string, num string) (ExamplepbSimpleMessage, error) { +func (a EchoServiceApi) Echo_1 (id string, num string) (ExamplepbSimpleMessage, error) { + + _sling := sling.New().Get(a.basePath) + + // create path and map variables + path := "/v1/example/echo/{id}/{num}" + path = strings.Replace(path, "{" + "id" + "}", fmt.Sprintf("%v", id), -1) + path = strings.Replace(path, "{" + "num" + "}", fmt.Sprintf("%v", num), -1) + + _sling = _sling.Path(path) + + // accept header + accepts := []string { "application/json" } + for key := range accepts { + _sling = _sling.Set("Accept", accepts[key]) + break // only use the first Accept + } + + + var successPayload = new(ExamplepbSimpleMessage) + + // We use this map (below) so that any arbitrary error JSON can be handled. + // FIXME: This is in the absence of this Go generator honoring the non-2xx + // response (error) models, which needs to be implemented at some point. + var failurePayload map[string]interface{} + + httpResponse, err := _sling.Receive(successPayload, &failurePayload) + + if err == nil { + // err == nil only means that there wasn't a sub-application-layer error (e.g. no network error) + if failurePayload != nil { + // If the failurePayload is present, there likely was some kind of non-2xx status + // returned (and a JSON payload error present) + var str []byte + str, err = json.Marshal(failurePayload) + if err == nil { // For safety, check for an error marshalling... probably superfluous + // This will return the JSON error body as a string + err = errors.New(string(str)) + } + } else { + // So, there was no network-type error, and nothing in the failure payload, + // but we should still check the status code + if httpResponse == nil { + // This should never happen... + err = errors.New("No HTTP Response received.") + } else if code := httpResponse.StatusCode; 200 > code || code > 299 { + err = errors.New("HTTP Error: " + string(httpResponse.StatusCode)) + } + } + } + + return *successPayload, err +} diff --git a/examples/clients/echo/ExamplepbSimpleMessage.go b/examples/clients/echo/ExamplepbSimpleMessage.go index ed9ada35b70..3b1fe9bc344 100644 --- a/examples/clients/echo/ExamplepbSimpleMessage.go +++ b/examples/clients/echo/ExamplepbSimpleMessage.go @@ -5,5 +5,6 @@ import ( type ExamplepbSimpleMessage struct { Id string `json:"id,omitempty"` + Num string `json:"num,omitempty"` } diff --git a/examples/examplepb/a_bit_of_everything.pb.gw.go b/examples/examplepb/a_bit_of_everything.pb.gw.go index 5e5415b5e55..ec3b01490a5 100644 --- a/examples/examplepb/a_bit_of_everything.pb.gw.go +++ b/examples/examplepb/a_bit_of_everything.pb.gw.go @@ -55,7 +55,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.FloatValue, err = runtime.Float32(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "float_value", err) } val, ok = pathParams["double_value"] @@ -66,7 +66,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.DoubleValue, err = runtime.Float64(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "double_value", err) } val, ok = pathParams["int64_value"] @@ -77,7 +77,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.Int64Value, err = runtime.Int64(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "int64_value", err) } val, ok = pathParams["uint64_value"] @@ -88,7 +88,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.Uint64Value, err = runtime.Uint64(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "uint64_value", err) } val, ok = pathParams["int32_value"] @@ -99,7 +99,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.Int32Value, err = runtime.Int32(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "int32_value", err) } val, ok = pathParams["fixed64_value"] @@ -110,7 +110,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.Fixed64Value, err = runtime.Uint64(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "fixed64_value", err) } val, ok = pathParams["fixed32_value"] @@ -121,7 +121,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.Fixed32Value, err = runtime.Uint32(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "fixed32_value", err) } val, ok = pathParams["bool_value"] @@ -132,7 +132,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.BoolValue, err = runtime.Bool(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "bool_value", err) } val, ok = pathParams["string_value"] @@ -143,7 +143,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.StringValue, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "string_value", err) } val, ok = pathParams["uint32_value"] @@ -154,7 +154,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.Uint32Value, err = runtime.Uint32(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "uint32_value", err) } val, ok = pathParams["sfixed32_value"] @@ -165,7 +165,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.Sfixed32Value, err = runtime.Int32(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "sfixed32_value", err) } val, ok = pathParams["sfixed64_value"] @@ -176,7 +176,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.Sfixed64Value, err = runtime.Int64(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "sfixed64_value", err) } val, ok = pathParams["sint32_value"] @@ -187,7 +187,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.Sint32Value, err = runtime.Int32(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "sint32_value", err) } val, ok = pathParams["sint64_value"] @@ -198,7 +198,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.Sint64Value, err = runtime.Int64(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "sint64_value", err) } val, ok = pathParams["nonConventionalNameValue"] @@ -209,7 +209,7 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, marshaler run protoReq.NonConventionalNameValue, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "nonConventionalNameValue", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_ABitOfEverythingService_Create_0); err != nil { @@ -253,7 +253,7 @@ func request_ABitOfEverythingService_Lookup_0(ctx context.Context, marshaler run protoReq.Uuid, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "uuid", err) } msg, err := client.Lookup(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) @@ -284,7 +284,7 @@ func request_ABitOfEverythingService_Update_0(ctx context.Context, marshaler run protoReq.Uuid, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "uuid", err) } msg, err := client.Update(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) @@ -311,7 +311,7 @@ func request_ABitOfEverythingService_Delete_0(ctx context.Context, marshaler run protoReq.Uuid, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "uuid", err) } msg, err := client.Delete(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) @@ -342,7 +342,7 @@ func request_ABitOfEverythingService_GetQuery_0(ctx context.Context, marshaler r protoReq.Uuid, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "uuid", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_ABitOfEverythingService_GetQuery_0); err != nil { @@ -373,7 +373,7 @@ func request_ABitOfEverythingService_Echo_0(ctx context.Context, marshaler runti protoReq.Value, err = runtime.StringP(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "value", err) } msg, err := client.Echo(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) @@ -434,7 +434,7 @@ func request_ABitOfEverythingService_DeepPathEcho_0(ctx context.Context, marshal err = runtime.PopulateFieldFromPath(&protoReq, "single_nested.name", val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "single_nested.name", err) } msg, err := client.DeepPathEcho(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) diff --git a/examples/examplepb/echo_service.pb.go b/examples/examplepb/echo_service.pb.go index 447758379c1..f4b5f3b5caa 100644 --- a/examples/examplepb/echo_service.pb.go +++ b/examples/examplepb/echo_service.pb.go @@ -50,7 +50,8 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package // SimpleMessage represents a simple message sent to the Echo service. type SimpleMessage struct { // Id represents the message identifier. - Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"` + Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"` + Num int64 `protobuf:"varint,2,opt,name=num" json:"num,omitempty"` } func (m *SimpleMessage) Reset() { *m = SimpleMessage{} } @@ -65,6 +66,13 @@ func (m *SimpleMessage) GetId() string { return "" } +func (m *SimpleMessage) GetNum() int64 { + if m != nil { + return m.Num + } + return 0 +} + func init() { proto.RegisterType((*SimpleMessage)(nil), "grpc.gateway.examples.examplepb.SimpleMessage") } @@ -187,20 +195,22 @@ var _EchoService_serviceDesc = grpc.ServiceDesc{ func init() { proto.RegisterFile("examples/examplepb/echo_service.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 229 bytes of a gzipped FileDescriptorProto + // 257 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x4d, 0xad, 0x48, 0xcc, 0x2d, 0xc8, 0x49, 0x2d, 0xd6, 0x87, 0x32, 0x0a, 0x92, 0xf4, 0x53, 0x93, 0x33, 0xf2, 0xe3, 0x8b, 0x53, 0x8b, 0xca, 0x32, 0x93, 0x53, 0xf5, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, 0x85, 0xe4, 0xd3, 0x8b, 0x0a, 0x92, 0xf5, 0xd2, 0x13, 0x4b, 0x52, 0xcb, 0x13, 0x2b, 0xf5, 0x60, 0x7a, 0xf4, 0xe0, 0x7a, 0xa4, 0x64, 0xd2, 0xf3, 0xf3, 0xd3, 0x73, 0x52, 0xf5, 0x13, 0x0b, 0x32, 0xf5, 0x13, 0xf3, 0xf2, - 0xf2, 0x4b, 0x12, 0x4b, 0x32, 0xf3, 0xf3, 0x8a, 0x21, 0xda, 0x95, 0xe4, 0xb9, 0x78, 0x83, 0x33, + 0xf2, 0x4b, 0x12, 0x4b, 0x32, 0xf3, 0xf3, 0x8a, 0x21, 0xda, 0x95, 0x0c, 0xb9, 0x78, 0x83, 0x33, 0x41, 0x2a, 0x7d, 0x53, 0x8b, 0x8b, 0x13, 0xd3, 0x53, 0x85, 0xf8, 0xb8, 0x98, 0x32, 0x53, 0x24, - 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0x98, 0x32, 0x53, 0x8c, 0x96, 0x30, 0x71, 0x71, 0xbb, 0x26, - 0x67, 0xe4, 0x07, 0x43, 0x6c, 0x15, 0x6a, 0x65, 0xe4, 0x62, 0x01, 0xf1, 0x85, 0xf4, 0xf4, 0x08, - 0xd8, 0xac, 0x87, 0x62, 0xb0, 0x14, 0x89, 0xea, 0x95, 0x64, 0x9b, 0x2e, 0x3f, 0x99, 0xcc, 0x24, - 0xae, 0x24, 0xaa, 0x5f, 0x66, 0x08, 0x0b, 0x02, 0x70, 0x00, 0xe8, 0x57, 0x67, 0xa6, 0xd4, 0x0a, - 0xf5, 0x30, 0x72, 0x71, 0x80, 0xdc, 0xe1, 0x94, 0x9f, 0x52, 0x49, 0x73, 0xb7, 0x28, 0x80, 0xdd, - 0x22, 0x85, 0xe9, 0x96, 0xf8, 0xa4, 0xfc, 0x94, 0x4a, 0x2b, 0x46, 0x2d, 0x27, 0xee, 0x28, 0x4e, - 0xb8, 0xe6, 0x24, 0x36, 0x70, 0xd8, 0x1a, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x26, 0x96, 0x37, - 0xac, 0xc3, 0x01, 0x00, 0x00, + 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0x98, 0x32, 0x53, 0x84, 0x04, 0xb8, 0x98, 0xf3, 0x4a, 0x73, + 0x25, 0x98, 0x14, 0x18, 0x35, 0x98, 0x83, 0x40, 0x4c, 0xa3, 0xc3, 0x4c, 0x5c, 0xdc, 0xae, 0xc9, + 0x19, 0xf9, 0xc1, 0x10, 0x77, 0x08, 0x2d, 0x61, 0xe4, 0x62, 0x01, 0xf1, 0x85, 0xf4, 0xf4, 0x08, + 0xb8, 0x45, 0x0f, 0xc5, 0x2a, 0x29, 0x12, 0xd5, 0x2b, 0xd9, 0x34, 0x5d, 0x7e, 0x32, 0x99, 0xc9, + 0x4c, 0x49, 0x54, 0xbf, 0xcc, 0x10, 0x16, 0x28, 0xe0, 0x20, 0xd1, 0xaf, 0xce, 0x4c, 0xa9, 0x8d, + 0x92, 0x15, 0x92, 0xc6, 0x2a, 0xa1, 0x5f, 0x9d, 0x57, 0x9a, 0x5b, 0x2b, 0xd4, 0xc3, 0xc8, 0xc5, + 0x01, 0x72, 0xa6, 0x53, 0x7e, 0x4a, 0x25, 0xcd, 0x9d, 0xaa, 0x00, 0x76, 0xaa, 0x14, 0xa6, 0x53, + 0xe3, 0x93, 0xf2, 0x53, 0x2a, 0xad, 0x18, 0xb5, 0x9c, 0xb8, 0xa3, 0x38, 0xe1, 0x9a, 0x93, 0xd8, + 0xc0, 0x91, 0x61, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0xf8, 0x6e, 0x70, 0xce, 0xf4, 0x01, 0x00, + 0x00, } diff --git a/examples/examplepb/echo_service.pb.gw.go b/examples/examplepb/echo_service.pb.gw.go index 70395244450..9710887fab2 100644 --- a/examples/examplepb/echo_service.pb.gw.go +++ b/examples/examplepb/echo_service.pb.gw.go @@ -29,6 +29,10 @@ var _ status.Status var _ = runtime.String var _ = utilities.NewDoubleArray +var ( + filter_EchoService_Echo_0 = &utilities.DoubleArray{Encoding: map[string]int{"id": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + func request_EchoService_Echo_0(ctx context.Context, marshaler runtime.Marshaler, client EchoServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq SimpleMessage var metadata runtime.ServerMetadata @@ -48,7 +52,49 @@ func request_EchoService_Echo_0(ctx context.Context, marshaler runtime.Marshaler protoReq.Id, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "id", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_EchoService_Echo_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.Echo(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func request_EchoService_Echo_1(ctx context.Context, marshaler runtime.Marshaler, client EchoServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq SimpleMessage + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["id"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "id") + } + + protoReq.Id, err = runtime.String(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "id", err) + } + + val, ok = pathParams["num"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "num") + } + + protoReq.Num, err = runtime.Int64(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "num", err) } msg, err := client.Echo(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) @@ -128,6 +174,35 @@ func RegisterEchoServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn }) + mux.Handle("GET", pattern_EchoService_Echo_1, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + if cn, ok := w.(http.CloseNotifier); ok { + go func(done <-chan struct{}, closed <-chan bool) { + select { + case <-done: + case <-closed: + cancel() + } + }(ctx.Done(), cn.CloseNotify()) + } + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_EchoService_Echo_1(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_EchoService_Echo_1(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + mux.Handle("POST", pattern_EchoService_EchoBody_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -163,11 +238,15 @@ func RegisterEchoServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn var ( pattern_EchoService_Echo_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 1, 0, 4, 1, 5, 3}, []string{"v1", "example", "echo", "id"}, "")) + pattern_EchoService_Echo_1 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 1, 0, 4, 1, 5, 3, 1, 0, 4, 1, 5, 4}, []string{"v1", "example", "echo", "id", "num"}, "")) + pattern_EchoService_EchoBody_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"v1", "example", "echo_body"}, "")) ) var ( forward_EchoService_Echo_0 = runtime.ForwardResponseMessage + forward_EchoService_Echo_1 = runtime.ForwardResponseMessage + forward_EchoService_EchoBody_0 = runtime.ForwardResponseMessage ) diff --git a/examples/examplepb/echo_service.proto b/examples/examplepb/echo_service.proto index 44555e8553f..0a7a6fa7f49 100644 --- a/examples/examplepb/echo_service.proto +++ b/examples/examplepb/echo_service.proto @@ -13,6 +13,7 @@ import "google/api/annotations.proto"; message SimpleMessage { // Id represents the message identifier. string id = 1; + int64 num = 2; } // Echo service responds to incoming echo requests. @@ -24,6 +25,9 @@ service EchoService { rpc Echo(SimpleMessage) returns (SimpleMessage) { option (google.api.http) = { post: "/v1/example/echo/{id}" + additional_bindings { + get: "/v1/example/echo/{id}/{num}" + } }; } // EchoBody method receives a simple message and returns it. diff --git a/examples/examplepb/echo_service.swagger.json b/examples/examplepb/echo_service.swagger.json index 9380472a3f9..c58fbc54cea 100644 --- a/examples/examplepb/echo_service.swagger.json +++ b/examples/examplepb/echo_service.swagger.json @@ -42,6 +42,39 @@ ] } }, + "/v1/example/echo/{id}/{num}": { + "get": { + "summary": "Echo method receives a simple message and returns it.", + "description": "The message posted as the id parameter will also be\nreturned.", + "operationId": "Echo", + "responses": { + "200": { + "description": "", + "schema": { + "$ref": "#/definitions/examplepbSimpleMessage" + } + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "type": "string" + }, + { + "name": "num", + "in": "path", + "required": true, + "type": "string", + "format": "int64" + } + ], + "tags": [ + "EchoService" + ] + } + }, "/v1/example/echo_body": { "post": { "summary": "EchoBody method receives a simple message and returns it.", @@ -77,6 +110,10 @@ "id": { "type": "string", "description": "Id represents the message identifier." + }, + "num": { + "type": "string", + "format": "int64" } }, "description": "SimpleMessage represents a simple message sent to the Echo service." diff --git a/examples/examplepb/flow_combination.pb.gw.go b/examples/examplepb/flow_combination.pb.gw.go index a18997ceb1e..9b1ae373c6e 100644 --- a/examples/examplepb/flow_combination.pb.gw.go +++ b/examples/examplepb/flow_combination.pb.gw.go @@ -180,7 +180,7 @@ func request_FlowCombination_RpcBodyRpc_1(ctx context.Context, marshaler runtime protoReq.A, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a", err) } val, ok = pathParams["b"] @@ -191,7 +191,7 @@ func request_FlowCombination_RpcBodyRpc_1(ctx context.Context, marshaler runtime protoReq.B, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "b", err) } val, ok = pathParams["c"] @@ -202,7 +202,7 @@ func request_FlowCombination_RpcBodyRpc_1(ctx context.Context, marshaler runtime protoReq.C, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "c", err) } msg, err := client.RpcBodyRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) @@ -250,7 +250,7 @@ func request_FlowCombination_RpcBodyRpc_3(ctx context.Context, marshaler runtime protoReq.A, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a", err) } val, ok = pathParams["b"] @@ -261,7 +261,7 @@ func request_FlowCombination_RpcBodyRpc_3(ctx context.Context, marshaler runtime protoReq.B, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "b", err) } msg, err := client.RpcBodyRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) @@ -317,7 +317,7 @@ func request_FlowCombination_RpcBodyRpc_5(ctx context.Context, marshaler runtime protoReq.A, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyRpc_5); err != nil { @@ -352,7 +352,7 @@ func request_FlowCombination_RpcBodyRpc_6(ctx context.Context, marshaler runtime protoReq.A, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyRpc_6); err != nil { @@ -387,7 +387,7 @@ func request_FlowCombination_RpcPathSingleNestedRpc_0(ctx context.Context, marsh err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a.str", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathSingleNestedRpc_0); err != nil { @@ -426,7 +426,7 @@ func request_FlowCombination_RpcPathNestedRpc_0(ctx context.Context, marshaler r err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a.str", err) } val, ok = pathParams["b"] @@ -437,7 +437,7 @@ func request_FlowCombination_RpcPathNestedRpc_0(ctx context.Context, marshaler r protoReq.B, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "b", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedRpc_0); err != nil { @@ -472,7 +472,7 @@ func request_FlowCombination_RpcPathNestedRpc_1(ctx context.Context, marshaler r err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a.str", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedRpc_1); err != nil { @@ -511,7 +511,7 @@ func request_FlowCombination_RpcPathNestedRpc_2(ctx context.Context, marshaler r err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a.str", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedRpc_2); err != nil { @@ -563,7 +563,7 @@ func request_FlowCombination_RpcBodyStream_1(ctx context.Context, marshaler runt protoReq.A, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a", err) } val, ok = pathParams["b"] @@ -574,7 +574,7 @@ func request_FlowCombination_RpcBodyStream_1(ctx context.Context, marshaler runt protoReq.B, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "b", err) } val, ok = pathParams["c"] @@ -585,7 +585,7 @@ func request_FlowCombination_RpcBodyStream_1(ctx context.Context, marshaler runt protoReq.C, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "c", err) } stream, err := client.RpcBodyStream(ctx, &protoReq) @@ -649,7 +649,7 @@ func request_FlowCombination_RpcBodyStream_3(ctx context.Context, marshaler runt protoReq.A, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a", err) } val, ok = pathParams["b"] @@ -660,7 +660,7 @@ func request_FlowCombination_RpcBodyStream_3(ctx context.Context, marshaler runt protoReq.B, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "b", err) } stream, err := client.RpcBodyStream(ctx, &protoReq) @@ -732,7 +732,7 @@ func request_FlowCombination_RpcBodyStream_5(ctx context.Context, marshaler runt protoReq.A, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyStream_5); err != nil { @@ -775,7 +775,7 @@ func request_FlowCombination_RpcBodyStream_6(ctx context.Context, marshaler runt protoReq.A, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyStream_6); err != nil { @@ -818,7 +818,7 @@ func request_FlowCombination_RpcPathSingleNestedStream_0(ctx context.Context, ma err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a.str", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathSingleNestedStream_0); err != nil { @@ -865,7 +865,7 @@ func request_FlowCombination_RpcPathNestedStream_0(ctx context.Context, marshale err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a.str", err) } val, ok = pathParams["b"] @@ -876,7 +876,7 @@ func request_FlowCombination_RpcPathNestedStream_0(ctx context.Context, marshale protoReq.B, err = runtime.String(val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "b", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedStream_0); err != nil { @@ -919,7 +919,7 @@ func request_FlowCombination_RpcPathNestedStream_1(ctx context.Context, marshale err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a.str", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedStream_1); err != nil { @@ -966,7 +966,7 @@ func request_FlowCombination_RpcPathNestedStream_2(ctx context.Context, marshale err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "a.str", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedStream_2); err != nil { diff --git a/examples/integration_test.go b/examples/integration_test.go index 710b8249623..44a9fa7da21 100644 --- a/examples/integration_test.go +++ b/examples/integration_test.go @@ -762,3 +762,23 @@ func TestMethodNotAllowed(t *testing.T) { t.Logf("%s", buf) } } + +func TestInvalidArgument(t *testing.T) { + url := "http://localhost:8080/v1/example/echo/myid/not_int64" + resp, err := http.Get(url) + if err != nil { + t.Errorf("http.Get(%q) failed with %v; want success", url, err) + return + } + defer resp.Body.Close() + buf, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Errorf("iotuil.ReadAll(resp.Body) failed with %v; want success", err) + return + } + + if got, want := resp.StatusCode, http.StatusBadRequest; got != want { + t.Errorf("resp.StatusCode = %d; want %d", got, want) + t.Logf("%s", buf) + } +} diff --git a/protoc-gen-grpc-gateway/gengateway/template.go b/protoc-gen-grpc-gateway/gengateway/template.go index c6fcc3bfe64..b660f16d6e0 100644 --- a/protoc-gen-grpc-gateway/gengateway/template.go +++ b/protoc-gen-grpc-gateway/gengateway/template.go @@ -228,7 +228,7 @@ var ( {{$param.RHS "protoReq"}}, err = {{$param.ConvertFuncExpr}}(val) {{end}} if err != nil { - return nil, metadata, err + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", {{$param | printf "%q"}}, err) } {{end}} {{end}}