Skip to content

Commit

Permalink
fix: detect pgx unique violations (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr authored May 14, 2020
1 parent 03559f9 commit e55212d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions sqlcon/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"strings"

"github.com/go-sql-driver/mysql"
"github.com/jackc/pgconn"
"github.com/lib/pq"
"github.com/pkg/errors"

"github.com/ory/herodot"

"github.com/ory/x/errorsx"
)

Expand Down Expand Up @@ -44,7 +45,19 @@ func HandleError(err error) error {
return errors.WithStack(ErrNoRows)
}

if err, ok := errorsx.Cause(err).(*pgconn.PgError); ok {
if e, ok := errorsx.Cause(err).(interface{
SQLState() string
}); ok {
switch e.SQLState() {
case "23505": // "unique_violation"
return errors.Wrap(ErrUniqueViolation, err.Error())
case "40001": // "serialization_failure"
return errors.Wrap(ErrConcurrentUpdate, err.Error())
}
return errors.WithStack(err)
}

if err, ok := errorsx.Cause(err).(*pq.Error); ok {
switch err.Code {
case "23505": // "unique_violation"
return errors.Wrap(ErrUniqueViolation, err.Error())
Expand Down

0 comments on commit e55212d

Please sign in to comment.