Files
apskel-pos-backend/internal/repository/pg_errors.go
T

26 lines
813 B
Go
Raw Normal View History

package repository
import (
"errors"
"github.com/jackc/pgx/v5/pgconn"
)
// isConstraintViolation reports whether err is Postgres refusing a row on the named
// constraint or unique index, with the given SQLSTATE.
func isConstraintViolation(err error, code, constraint string) bool {
var pgErr *pgconn.PgError
return errors.As(err, &pgErr) && pgErr.Code == code && pgErr.ConstraintName == constraint
}
func isUniqueViolation(err error, constraint string) bool {
return isConstraintViolation(err, "23505", constraint)
}
// isForeignKeyViolation reports whether err is a row still referenced from elsewhere,
// or pointing at one that does not exist, whatever the constraint.
func isForeignKeyViolation(err error) bool {
var pgErr *pgconn.PgError
return errors.As(err, &pgErr) && pgErr.Code == "23503"
}