Skip to content

Commit c787987

Browse files
committed
fix: user errors.As for error type assertion
1 parent 7a4e302 commit c787987

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

crdb/error.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@ package crdb
1616

1717
import "fmt"
1818

19-
// errorCause returns the original cause of the error, if possible. An
20-
// error has a proximate cause if it's type is compatible with Go's
21-
// errors.Unwrap() or pkg/errors' Cause(); the original cause is the
22-
// end of the causal chain.
23-
func errorCause(err error) error {
24-
for err != nil {
25-
if c, ok := err.(interface{ Cause() error }); ok {
26-
err = c.Cause()
27-
} else if c, ok := err.(interface{ Unwrap() error }); ok {
28-
err = c.Unwrap()
29-
} else {
30-
break
31-
}
32-
}
33-
return err
34-
}
35-
3619
type txError struct {
3720
cause error
3821
}

crdb/tx.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package crdb
1919
import (
2020
"context"
2121
"database/sql"
22+
"errors"
2223

2324
"github.com/lib/pq"
2425
)
@@ -193,16 +194,17 @@ func errIsRetryable(err error) bool {
193194
}
194195

195196
func errCode(err error) string {
196-
switch t := errorCause(err).(type) {
197-
case *pq.Error:
198-
return string(t.Code)
199-
200-
case errWithSQLState:
201-
return t.SQLState()
197+
var pqe *pq.Error
198+
if errors.As(err, &pqe) {
199+
return string(pqe.Code)
200+
}
202201

203-
default:
204-
return ""
202+
var sqlErr errWithSQLState
203+
if errors.As(err, &sqlErr) {
204+
return sqlErr.SQLState()
205205
}
206+
207+
return ""
206208
}
207209

208210
// errWithSQLState is implemented by pgx (pgconn.PgError).

0 commit comments

Comments
 (0)