-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors.go
86 lines (76 loc) · 2.89 KB
/
errors.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
76
77
78
79
80
81
82
83
84
85
86
package pg
import (
"fmt"
"strings"
"github.com/jackc/pgx/v5"
)
var (
// ErrNoRows is fired from a query when no results are came back.
// Usually it's ignored and an empty json array is sent to the client instead.
ErrNoRows = pgx.ErrNoRows
)
// IsErrDuplicate reports whether the return error from `Insert` method
// was caused because of a violation of a unique constraint (it's not typed error at the underline driver).
// It returns the constraint key if it's true.
func IsErrDuplicate(err error) (string, bool) {
if err != nil {
errText := err.Error()
if strings.Contains(errText, "ERROR: duplicate key value violates unique constraint") {
if startIdx := strings.IndexByte(errText, '"'); startIdx > 0 && startIdx+1 < len(errText) {
errText = errText[startIdx+1:]
if endIdx := strings.IndexByte(errText, '"'); endIdx > 0 && endIdx < len(errText) {
return errText[:endIdx], true
}
}
}
}
return "", false
}
// IsErrForeignKey reports whether an insert or update command failed due
// to an invalid foreign key: a foreign key is missing or its source was not found.
// E.g. ERROR: insert or update on table "food_user_friendly_units" violates foreign key constraint "fk_food" (SQLSTATE 23503)
func IsErrForeignKey(err error) (string, bool) {
if err != nil {
errText := err.Error()
if strings.Contains(errText, "violates foreign key constraint") {
if startIdx := strings.IndexByte(errText, '"'); startIdx > 0 && startIdx+1 < len(errText) {
errText = errText[startIdx+1:]
if endIdx := strings.IndexByte(errText, '"'); endIdx > 0 && endIdx < len(errText) {
return errText[:endIdx], true
}
}
}
}
return "", false
}
// IsErrInputSyntax reports whether the return error from `Insert` method
// was caused because of invalid input syntax for a specific postgres column type.
func IsErrInputSyntax(err error) (string, bool) {
if err != nil {
errText := err.Error()
if strings.HasPrefix(errText, "ERROR: ") {
if strings.Contains(errText, "ERROR: invalid input syntax for type") || strings.Contains(errText, "ERROR: syntax error in tsquery") || strings.Contains(errText, "ERROR: no operand in tsquery") {
if startIdx := strings.IndexByte(errText, '"'); startIdx > 0 && startIdx+1 < len(errText) {
errText = errText[startIdx+1:]
if endIdx := strings.IndexByte(errText, '"'); endIdx > 0 && endIdx < len(errText) {
return errText[:endIdx], true
}
} else {
// more generic error.
return "invalid input syntax", true
}
}
}
}
return "", false
}
// IsErrColumnNotExists reports whether the error is caused because the "col" defined
// in a select query was not exists in a row.
// There is no a typed error available in the driver itself.
func IsErrColumnNotExists(err error, col string) bool {
if err == nil {
return false
}
errText := fmt.Sprintf(`column "%s" does not exist`, col)
return strings.Contains(err.Error(), errText)
}