Files
mtusi2026lab2/internal/storage/postgres/errors.go
T
Vladislav Kunetskiy 1e38dbdf35
Application / Lint (pull_request) Canceled after 0s
Application / Unit tests (pull_request) Canceled after 0s
Application / Docker build (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
lab2
2026-10-06 18:58:40 +03:00

43 lines
694 B
Go

package postgres
import (
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"mtusi-messenger/internal/domain"
)
const (
pgUniqueViolation = "23505"
pgCheckViolation = "23514"
)
func isUniqueViolation(err error) bool {
var pgErr *pgconn.PgError
return errors.As(err, &pgErr) && pgErr.Code == pgUniqueViolation
}
func mapError(err error) error {
if err == nil {
return nil
}
if errors.Is(err, pgx.ErrNoRows) {
return domain.ErrNotFound
}
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
switch pgErr.Code {
case pgUniqueViolation:
return domain.ErrConflict
case pgCheckViolation:
return domain.ErrValidation
}
}
return err
}