From 804bd338324d7a520bb1c6f8446504b799eff725 Mon Sep 17 00:00:00 2001 From: fblaha Date: Tue, 1 Oct 2019 16:22:31 +0300 Subject: [PATCH] Revert "handling serialization failure during transaction commit (#174)" This reverts commit a0eee56f --- gorm/transaction.go | 14 +------------- gorm/transaction_test.go | 35 ----------------------------------- 2 files changed, 1 insertion(+), 48 deletions(-) diff --git a/gorm/transaction.go b/gorm/transaction.go index aa989c3c..991bc5c6 100644 --- a/gorm/transaction.go +++ b/gorm/transaction.go @@ -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" @@ -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") } } @@ -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") -} diff --git a/gorm/transaction_test.go b/gorm/transaction_test.go index 90e0473a..0d498f9c 100644 --- a/gorm/transaction_test.go +++ b/gorm/transaction_test.go @@ -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" ) @@ -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()) - }) - } -}