fix(postgres): map driver errors and keep shared pool after migrations

This commit is contained in:
Голиков Maximillian Антон
2026-10-02 08:55:21 +03:00
parent 322ef09c1f
commit 45b3606259
2 changed files with 27 additions and 2 deletions
-1
View File
@@ -62,7 +62,6 @@ func Connect(ctx context.Context, cfg Config) (*pgxpool.Pool, error) {
func RunMigrations(ctx context.Context, pool *pgxpool.Pool) error { func RunMigrations(ctx context.Context, pool *pgxpool.Pool) error {
db := stdlib.OpenDBFromPool(pool) db := stdlib.OpenDBFromPool(pool)
defer db.Close()
goose.SetBaseFS(migrations.FS) goose.SetBaseFS(migrations.FS)
if err := goose.SetDialect("postgres"); err != nil { if err := goose.SetDialect("postgres"); err != nil {
+27 -1
View File
@@ -1,6 +1,7 @@
package postgres package postgres
import ( import (
"context"
"errors" "errors"
"github.com/jackc/pgerrcode" "github.com/jackc/pgerrcode"
@@ -10,11 +11,36 @@ import (
"high-loud-apps-labs/internal/domain" "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 { func notFound(err error, entity, id string) error {
if err == nil {
return nil
}
if errors.Is(err, pgx.ErrNoRows) { if errors.Is(err, pgx.ErrNoRows) {
return &domain.NotFoundError{Entity: entity, ID: id} return &domain.NotFoundError{Entity: entity, ID: id}
} }
return err return mapError(err)
} }
func uniqueViolation(err error) bool { func uniqueViolation(err error) bool {