diff --git a/backend/internal/storage/postgres/db.go b/backend/internal/storage/postgres/db.go index d1f406e..875a88c 100644 --- a/backend/internal/storage/postgres/db.go +++ b/backend/internal/storage/postgres/db.go @@ -62,7 +62,6 @@ func Connect(ctx context.Context, cfg Config) (*pgxpool.Pool, error) { func RunMigrations(ctx context.Context, pool *pgxpool.Pool) error { db := stdlib.OpenDBFromPool(pool) - defer db.Close() goose.SetBaseFS(migrations.FS) if err := goose.SetDialect("postgres"); err != nil { diff --git a/backend/internal/storage/postgres/errors.go b/backend/internal/storage/postgres/errors.go index 905d0bf..a3a4de3 100644 --- a/backend/internal/storage/postgres/errors.go +++ b/backend/internal/storage/postgres/errors.go @@ -1,6 +1,7 @@ package postgres import ( + "context" "errors" "github.com/jackc/pgerrcode" @@ -10,11 +11,36 @@ import ( "high-loud-apps-labs/internal/domain" ) +type coded interface { + Code() domain.ErrorCode +} + +// mapError переводит ошибки драйвера в доменные. +func mapError(err error) error { + if err == nil { + return nil + } + var c coded + if errors.As(err, &c) { + return err + } + if errors.Is(err, pgx.ErrNoRows) { + return err + } + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + return err + } + return &domain.UnavailableError{Reason: err.Error()} +} + func notFound(err error, entity, id string) error { + if err == nil { + return nil + } if errors.Is(err, pgx.ErrNoRows) { return &domain.NotFoundError{Entity: entity, ID: id} } - return err + return mapError(err) } func uniqueViolation(err error) bool {