Skip to content

Commit aa1fb71

Browse files
authored
gapic: wrap errors from REST better (#745)
1 parent b2bc7bc commit aa1fb71

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

internal/gengapic/genrest.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ func (g *generator) pagingRESTCall(servName string, m *descriptor.MethodDescript
608608
p(" }")
609609
p(" defer httpRsp.Body.Close()")
610610
p("")
611-
p(" if httpRsp.StatusCode != http.StatusOK {")
612-
p(` return nil, "", fmt.Errorf(httpRsp.Status)`)
611+
p(" if err = googleapi.CheckResponse(httpRsp); err != nil {")
612+
p(` return nil, "", err`)
613613
p(" }")
614614
p("")
615615
p(" buf, err := ioutil.ReadAll(httpRsp.Body)")
@@ -629,6 +629,7 @@ func (g *generator) pagingRESTCall(servName string, m *descriptor.MethodDescript
629629
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/iterator"}] = true
630630
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/protobuf/proto"}] = true
631631
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/protobuf/encoding/protojson"}] = true
632+
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/googleapi"}] = true
632633
g.imports[inSpec] = true
633634
g.imports[outSpec] = true
634635

@@ -743,13 +744,12 @@ func (g *generator) emptyUnaryRESTCall(servName string, m *descriptor.MethodDesc
743744
p("}")
744745
p("defer httpRsp.Body.Close()")
745746
p("")
746-
p("if httpRsp.StatusCode != http.StatusOK {")
747-
p(" return fmt.Errorf(httpRsp.Status)")
748-
p("}")
749-
p("")
750-
p("return nil")
747+
p("// Returns nil if there is no error, otherwise wraps")
748+
p("// the response code and body into a non-nil error")
749+
p("return googleapi.CheckResponse(httpRsp)")
751750
p("}")
752751

752+
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/googleapi"}] = true
753753
g.imports[inSpec] = true
754754
return nil
755755
}
@@ -845,8 +845,8 @@ func (g *generator) unaryRESTCall(servName string, m *descriptor.MethodDescripto
845845
p("}")
846846
p("defer httpRsp.Body.Close()")
847847
p("")
848-
p("if httpRsp.StatusCode != http.StatusOK {")
849-
p(" return nil, fmt.Errorf(httpRsp.Status)")
848+
p("if err = googleapi.CheckResponse(httpRsp); err != nil {")
849+
p(" return nil, err")
850850
p("}")
851851
p("")
852852
p("buf, err := ioutil.ReadAll(httpRsp.Body)")
@@ -867,10 +867,9 @@ func (g *generator) unaryRESTCall(servName string, m *descriptor.MethodDescripto
867867
}
868868
p(ret)
869869
p("}")
870-
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/protobuf/encoding/protojson"}] = true
871-
g.imports[inSpec] = true
872-
g.imports[outSpec] = true
873870

871+
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/googleapi"}] = true
872+
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/protobuf/encoding/protojson"}] = true
874873
g.imports[inSpec] = true
875874
g.imports[outSpec] = true
876875
return nil

internal/gengapic/genrest_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ func TestGenRestMethod(t *testing.T) {
481481
options: &options{diregapic: true},
482482
imports: map[pbinfo.ImportSpec]bool{
483483
{Path: "google.golang.org/protobuf/encoding/protojson"}: true,
484+
{Path: "google.golang.org/api/googleapi"}: true,
484485
{Name: "foopb", Path: "google.golang.org/genproto/cloud/foo/v1"}: true,
485486
},
486487
},

internal/gengapic/testdata/rest_CustomOp.want

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func (c *fooRESTClient) CustomOp(ctx context.Context, req *foopb.Foo, opts ...ga
2020
}
2121
defer httpRsp.Body.Close()
2222

23-
if httpRsp.StatusCode != http.StatusOK {
24-
return nil, fmt.Errorf(httpRsp.Status)
23+
if err = googleapi.CheckResponse(httpRsp); err != nil {
24+
return nil, err
2525
}
2626

2727
buf, err := ioutil.ReadAll(httpRsp.Body)

showcase/echo_test.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package showcase
1717
import (
1818
"context"
1919
"io"
20+
"net/http"
2021
"os"
2122
"strings"
2223
"testing"
@@ -25,6 +26,7 @@ import (
2526
"github.com/google/go-cmp/cmp"
2627
showcase "github.com/googleapis/gapic-showcase/client"
2728
showcasepb "github.com/googleapis/gapic-showcase/server/genproto"
29+
"google.golang.org/api/googleapi"
2830
"google.golang.org/api/iterator"
2931
"google.golang.org/api/option"
3032
locationpb "google.golang.org/genproto/googleapis/cloud/location"
@@ -68,19 +70,25 @@ func TestEcho_error(t *testing.T) {
6870
},
6971
}
7072
for typ, client := range map[string]*showcase.EchoClient{"grpc": echo, "rest": echoREST} {
71-
if typ == "rest" {
72-
// TODO(dovs): currently erroring with 2, want 1
73-
continue
74-
}
75-
7673
resp, err := client.Echo(context.Background(), req)
77-
if resp != nil {
74+
if resp != nil || err == nil {
7875
t.Errorf("%s Echo() = %v, wanted error %d", typ, resp, val)
7976
}
80-
status, _ := status.FromError(err)
81-
if status.Code() != val {
82-
t.Errorf("%s Echo() errors with %d, want %d", typ, status.Code(), val)
77+
if typ == "grpc" {
78+
status, _ := status.FromError(err)
79+
if status.Code() != val {
80+
t.Errorf("%s Echo() errors with %d, want %d", typ, status.Code(), val)
81+
}
82+
} else {
83+
const ERR_CODE int = http.StatusInternalServerError
84+
err, ok := err.(*googleapi.Error)
85+
if !ok {
86+
t.Errorf("%s Echo() returned unexpected error type: %v", typ, err)
87+
} else if err.Code != ERR_CODE {
88+
t.Errorf("%s Echo() errors with %d, want %d", typ, err.Code, ERR_CODE)
89+
}
8390
}
91+
8492
}
8593

8694
}

0 commit comments

Comments
 (0)