Skip to content

Commit

Permalink
pgx err wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
pai0id committed Jul 15, 2024
1 parent 10dbf26 commit bbc287d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions internal/infrastructure/postgres/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
ErrPostgresNotFoundError = errors.New(TextPostgresNotFoundError)
)

func mapPostgresError(code string) error {
func mapPgError(code string) error {
if code == uniqueConstraintViolation {
return ErrPostgresUniqueConstraintViolation
}
Expand All @@ -40,13 +40,26 @@ func mapPostgresError(code string) error {
return ErrPostgresUnknownError
}

func mapPgxError(err error) error {
if err == nil {
return nil
}
if err == pgx.ErrNoRows {
return ErrPostgresNotFoundError
}
return nil
}

func wrapPostgresError(err error) error {
if err == nil {
return nil
}
perr, ok := err.(pgx.PgError)
if pgxErr := mapPgxError(err); pgxErr != nil {
return fmt.Errorf("%w: %w", pgxErr, err)
}
pErr, ok := err.(pgx.PgError)
if !ok {
return err
}
return fmt.Errorf("%s %w: %w", perr.Code, mapPostgresError(perr.Code), err)
return fmt.Errorf("%s %w: %w", pErr.Code, mapPgError(pErr.Code), err)
}

0 comments on commit bbc287d

Please sign in to comment.