generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
dalerrs.go
55 lines (50 loc) · 1.67 KB
/
dalerrs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Package dalerrs provides common error handling utilities for all domain-specific DALs,
// e.g. controller DAL and configuration DAL, which all connect to the same underlying DB
// and maintain the same interface guarantees
package dalerrs
import (
stdsql "database/sql"
"errors"
"fmt"
"strings"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
var (
// ErrConflict is returned by select methods in the DAL when a resource already exists.
//
// Its use will be documented in the corresponding methods.
ErrConflict = errors.New("conflict")
// ErrNotFound is returned by select methods in the DAL when no results are found.
ErrNotFound = errors.New("not found")
// ErrConstraint is returned by select methods in the DAL when a constraint is violated.
ErrConstraint = errors.New("constraint violation")
)
func IsNotFound(err error) bool {
return errors.Is(err, stdsql.ErrNoRows) || errors.Is(err, pgx.ErrNoRows)
}
func TranslatePGError(err error) error {
if err == nil {
return nil
}
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
switch pgErr.Code {
case pgerrcode.ForeignKeyViolation:
return fmt.Errorf("%s: %w", strings.TrimSuffix(strings.TrimPrefix(pgErr.ConstraintName, pgErr.TableName+"_"), "_id_fkey"), ErrNotFound)
case pgerrcode.UniqueViolation:
return fmt.Errorf("%s: %w", pgErr.Message, ErrConflict)
case pgerrcode.IntegrityConstraintViolation,
pgerrcode.RestrictViolation,
pgerrcode.NotNullViolation,
pgerrcode.CheckViolation,
pgerrcode.ExclusionViolation:
return fmt.Errorf("%s: %w", pgErr.Message, ErrConstraint)
default:
}
} else if IsNotFound(err) {
return ErrNotFound
}
return err
}