-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
75 lines (62 loc) · 1.35 KB
/
error.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package pgxpoolgo
import (
"errors"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
const ErrDBCodeColumnNotExists = "42703"
const ErrDBCodeDuplicateKey = "23505"
const ErrDBCodeInvalidInputSyntax = "22P02"
type ErrDatabase struct {
DBErr error
DBCode string
DBMessage string
}
func ErrDB(e error) *ErrDatabase {
err := &ErrDatabase{
DBErr: e,
}
err.extract()
return err
}
func NewMockErrDB(code string) error {
err := &ErrDatabase{
DBErr: errors.New(code),
DBCode: code,
}
return err
}
func (e *ErrDatabase) Error() string {
return e.DBErr.Error()
}
func (e *ErrDatabase) Code() string {
return e.DBCode
}
func (e *ErrDatabase) Message() string {
return e.DBMessage
}
func (e *ErrDatabase) IsNoRows() bool {
return e.DBErr.Error() == pgx.ErrNoRows.Error()
}
func (e *ErrDatabase) IsColumnNotExists() bool {
return e.DBCode == ErrDBCodeColumnNotExists
}
func (e *ErrDatabase) IsDuplicateKey() bool {
return e.DBCode == ErrDBCodeDuplicateKey
}
func (e *ErrDatabase) IsInvalidInputSyntax() bool {
return e.DBCode == ErrDBCodeInvalidInputSyntax
}
func (e *ErrDatabase) extract() {
var pgErr *pgconn.PgError
if errors.As(e.DBErr, &pgErr) {
e.DBCode = pgErr.Code
e.DBMessage = pgErr.Message
} else {
var errDB *ErrDatabase
if errors.As(e.DBErr, &errDB) {
e.DBCode = errDB.DBCode
e.DBMessage = errDB.DBMessage
}
}
}