39 lines
1.2 KiB
Go
39 lines
1.2 KiB
Go
|
|
package postgres
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
|
||
|
|
"github.com/jackc/pgerrcode"
|
||
|
|
"github.com/jackc/pgx/v5"
|
||
|
|
"github.com/jackc/pgx/v5/pgconn"
|
||
|
|
|
||
|
|
"code.nochebuena.dev/go/xerrors"
|
||
|
|
)
|
||
|
|
|
||
|
|
// HandleError maps pgx and PostgreSQL errors to xerrors types.
|
||
|
|
// Also available as client.HandleError(err).
|
||
|
|
func HandleError(err error) error {
|
||
|
|
if err == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
var pgErr *pgconn.PgError
|
||
|
|
if errors.As(err, &pgErr) {
|
||
|
|
switch pgErr.Code {
|
||
|
|
case pgerrcode.UniqueViolation:
|
||
|
|
return xerrors.New(xerrors.ErrAlreadyExists, "record already exists").
|
||
|
|
WithContext("constraint", pgErr.ConstraintName).WithError(err)
|
||
|
|
case pgerrcode.ForeignKeyViolation:
|
||
|
|
return xerrors.New(xerrors.ErrInvalidInput, "data integrity violation").
|
||
|
|
WithContext("table", pgErr.TableName).WithError(err)
|
||
|
|
case pgerrcode.CheckViolation:
|
||
|
|
return xerrors.New(xerrors.ErrInvalidInput, "data constraint violation").
|
||
|
|
WithContext("table", pgErr.TableName).
|
||
|
|
WithContext("column", pgErr.ColumnName).WithError(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
||
|
|
return xerrors.New(xerrors.ErrNotFound, "record not found").WithError(err)
|
||
|
|
}
|
||
|
|
return xerrors.New(xerrors.ErrInternal, "unexpected database error").WithError(err)
|
||
|
|
}
|