diff --git a/crdb/error.go b/crdb/error.go index 5654ba9..be18fcd 100644 --- a/crdb/error.go +++ b/crdb/error.go @@ -16,23 +16,6 @@ package crdb import "fmt" -// errorCause returns the original cause of the error, if possible. An -// error has a proximate cause if it's type is compatible with Go's -// errors.Unwrap() or pkg/errors' Cause(); the original cause is the -// end of the causal chain. -func errorCause(err error) error { - for err != nil { - if c, ok := err.(interface{ Cause() error }); ok { - err = c.Cause() - } else if c, ok := err.(interface{ Unwrap() error }); ok { - err = c.Unwrap() - } else { - break - } - } - return err -} - type txError struct { cause error } diff --git a/crdb/tx.go b/crdb/tx.go index dffa72d..07e78ad 100644 --- a/crdb/tx.go +++ b/crdb/tx.go @@ -19,6 +19,7 @@ package crdb import ( "context" "database/sql" + "errors" "github.com/lib/pq" ) @@ -193,16 +194,17 @@ func errIsRetryable(err error) bool { } func errCode(err error) string { - switch t := errorCause(err).(type) { - case *pq.Error: - return string(t.Code) - - case errWithSQLState: - return t.SQLState() + var pqe *pq.Error + if errors.As(err, &pqe) { + return string(pqe.Code) + } - default: - return "" + var sqlErr errWithSQLState + if errors.As(err, &sqlErr) { + return sqlErr.SQLState() } + + return "" } // errWithSQLState is implemented by pgx (pgconn.PgError).