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

Fixed to use apierror.APIError instead of spanner.Error #90

Merged
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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ require (
github.com/MakeNowJust/memefish v0.0.0-20211014154734-dbfb8b28907d
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813
github.com/google/go-cmp v0.5.6
github.com/googleapis/gax-go/v2 v2.1.1
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15
github.com/jessevdk/go-assets-builder v0.0.0-20130903091706-b8483521738f // indirect
github.com/jessevdk/go-flags v1.5.0 // indirect
github.com/jinzhu/inflection v1.0.0
github.com/kenshaw/snaker v0.1.6
github.com/spf13/cobra v1.2.1
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect
golang.org/x/tools v0.1.8
google.golang.org/api v0.61.0
google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 h1:cW/amwGEJK5MSKntPXRjX4dxs/nGxGT8gXKIsKFmHGc=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15/go.mod h1:Fdm/oWRW+CH8PRbLntksCNtmcCBximKPkVQYvmMl80k=
github.com/jessevdk/go-assets-builder v0.0.0-20130903091706-b8483521738f h1:K2zqtTU3T3ZX/vVeFtJ1OoxEm+gsLhu3zQ34tKgOAyk=
github.com/jessevdk/go-assets-builder v0.0.0-20130903091706-b8483521738f/go.mod h1:GjkD6wGIxVEccQ4pa27Ebe00zAi1EEpAcL6rL0ADvwU=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -468,6 +472,8 @@ golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f h1:8w7RhxzTVgUzw/AH/9mUV5q0vMgy40SQRursCcfmkCw=
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
6 changes: 3 additions & 3 deletions templates/yo_db.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func (e yoError) DBTableName() string {
// If the error is originated from the Spanner library, this returns a gRPC status of
// the original error. It may contain details of the status such as RetryInfo.
func (e yoError) GRPCStatus() *status.Status {
var se *spanner.Error
if errors.As(e.err, &se) {
return status.Convert(se.Unwrap())
var ae *apierror.APIError
if errors.As(e.err, &ae) {
return status.Convert(ae.Unwrap())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apierror.APIError implements GRPCStatus() so it is enough to just call status.Convert(ae) without unwrap. (not sure why we use unwrap for spanner.Error)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out. I have corrected the issue in 464537e .

}

return status.New(e.code, e.Error())
Expand Down
9 changes: 5 additions & 4 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"cloud.google.com/go/civil"
"cloud.google.com/go/spanner"
"github.com/google/go-cmp/cmp"
"github.com/googleapis/gax-go/v2/apierror"
"go.mercari.io/yo/test/testmodels/customtypes"
models "go.mercari.io/yo/test/testmodels/default"
"go.mercari.io/yo/test/testutil"
Expand Down Expand Up @@ -697,12 +698,12 @@ func TestSessionNotFound(t *testing.T) {

t.Run("ConvertToSpannerError", func(t *testing.T) {
_, err = models.FindCompositePrimaryKey(ctx, client.Single(), "x200", 200)
var se *spanner.Error
if !errors.As(err, &se) {
t.Fatalf("the error returned by yo can be spanner.Error: %T", err)
var ae *apierror.APIError
if !errors.As(err, &ae) {
t.Fatalf("the error returned by yo can be apierror.APIError: %T", err)
}

st := status.Convert(se.Unwrap())
st := status.Convert(ae.Unwrap())
ri := extractResourceInfo(st)

expectedResourceInfo := &errdetails.ResourceInfo{
Expand Down
7 changes: 4 additions & 3 deletions test/testmodels/customtypes/yo_db.yo.go

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

7 changes: 4 additions & 3 deletions test/testmodels/default/yo_db.yo.go

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

7 changes: 4 additions & 3 deletions test/testmodels/single/single_file.go

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

30 changes: 15 additions & 15 deletions tplbin/templates.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions v2/module/builtin/templates/yo_db.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func (e yoError) DBTableName() string {
// If the error is originated from the Spanner library, this returns a gRPC status of
// the original error. It may contain details of the status such as RetryInfo.
func (e yoError) GRPCStatus() *status.Status {
var se *spanner.Error
if errors.As(e.err, &se) {
return status.Convert(se.Unwrap())
var ae *apierror.APIError
if errors.As(e.err, &ae) {
return status.Convert(ae.Unwrap())
}

return status.New(e.code, e.Error())
Expand Down
9 changes: 5 additions & 4 deletions v2/test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"cloud.google.com/go/civil"
"cloud.google.com/go/spanner"
"github.com/google/go-cmp/cmp"
"github.com/googleapis/gax-go/v2/apierror"
default_models "go.mercari.io/yo/v2/test/testmodels/default"
legacy_models "go.mercari.io/yo/v2/test/testmodels/legacy_default"
"go.mercari.io/yo/v2/test/testutil"
Expand Down Expand Up @@ -1147,12 +1148,12 @@ func TestSessionNotFound(t *testing.T) {

t.Run("ConvertToSpannerError", func(t *testing.T) {
_, err = default_models.FindCompositePrimaryKey(ctx, client.Single(), "x200", 200)
var se *spanner.Error
if !errors.As(err, &se) {
t.Fatalf("the error returned by yo can be spanner.Error: %T", err)
var ae *apierror.APIError
if !errors.As(err, &ae) {
t.Fatalf("the error returned by yo can be apierror.APIError: %T", err)
}

st := status.Convert(se.Unwrap())
st := status.Convert(ae.Unwrap())
ri := extractResourceInfo(st)

expectedResourceInfo := &errdetails.ResourceInfo{
Expand Down
7 changes: 4 additions & 3 deletions v2/test/testmodels/default/yo_db.yo.go

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

7 changes: 4 additions & 3 deletions v2/test/testmodels/legacy_default/yo_db.yo.go

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