Skip to content

Commit

Permalink
Revert "handling serialization failure during transaction commit (#174)"
Browse files Browse the repository at this point in the history
This reverts commit a0eee56
  • Loading branch information
fblaha committed Oct 1, 2019
1 parent a0eee56 commit 804bd33
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 48 deletions.
14 changes: 1 addition & 13 deletions gorm/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/infobloxopen/atlas-app-toolkit/rpc/errdetails"
"github.com/jinzhu/gorm"
"github.com/lib/pq"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -148,7 +147,7 @@ func UnaryServerInterceptor(db *gorm.DB) grpc.UnaryServerInterceptor {
terr = txn.Rollback()
} else {
if terr = txn.Commit(ctx); terr != nil {
err = translateCommitError(terr)
err = status.Error(codes.Internal, "failed to commit transaction")
}
}

Expand Down Expand Up @@ -176,14 +175,3 @@ func UnaryServerInterceptor(db *gorm.DB) grpc.UnaryServerInterceptor {
return resp, err
}
}

func translateCommitError(err error) error {
switch pgErr := err.(type) {
case *pq.Error:
switch pgErr.Code.Name() {
case "serialization_failure":
return status.Error(codes.Aborted, "failed to commit transaction due to collision with other transaction")
}
}
return status.Error(codes.Internal, "failed to commit transaction")
}
35 changes: 0 additions & 35 deletions gorm/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ package gorm
import (
"context"
"errors"
"fmt"
"reflect"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/infobloxopen/atlas-app-toolkit/rpc/errdetails"
"github.com/jinzhu/gorm"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -351,35 +348,3 @@ func TestBeginFromContext_Bad(t *testing.T) {
}

}

func TestTranslateCommitError(t *testing.T) {
r := require.New(t)
tests := []struct {
name string
inputErr error
wantedCode codes.Code
}{
{
name: "unknown error",
inputErr: fmt.Errorf("some error"),
wantedCode: codes.Internal,
},
{
name: "unknown PG error",
inputErr: &pq.Error{Code: "40005"},
wantedCode: codes.Internal,
},
{
name: "serialization failure",
inputErr: &pq.Error{Code: "40001"},
wantedCode: codes.Aborted,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, ok := status.FromError(translateCommitError(tt.inputErr))
r.True(ok)
r.Equal(tt.wantedCode, s.Code())
})
}
}

0 comments on commit 804bd33

Please sign in to comment.