@@ -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
0 commit comments