forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
64 lines (54 loc) · 1.28 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
package pg
import (
"io"
"net"
"gopkg.in/pg.v4/internal"
)
var (
ErrNoRows = internal.ErrNoRows
ErrMultiRows = internal.ErrMultiRows
errSSLNotSupported = internal.Errorf("pg: SSL is not enabled on the server")
errClosed = internal.Errorf("pg: database is closed")
errTxDone = internal.Errorf("pg: transaction has already been committed or rolled back")
errStmtClosed = internal.Errorf("pg: statement is closed")
errListenerClosed = internal.Errorf("pg: listener is closed")
)
type Error interface {
Field(byte) string
IntegrityViolation() bool
}
var _ Error = (*internal.PGError)(nil)
func isBadConn(err error, allowTimeout bool) bool {
if err == nil {
return false
}
if _, ok := err.(internal.Error); ok {
return false
}
if pgErr, ok := err.(Error); ok && pgErr.Field('S') != "FATAL" {
return false
}
if allowTimeout {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
return false
}
}
return true
}
func isNetworkError(err error) bool {
if err == io.EOF {
return true
}
_, ok := err.(net.Error)
return ok
}
func shouldRetry(err error) bool {
if err == nil {
return false
}
// 40001 - serialization_failure
if pgerr, ok := err.(Error); ok && pgerr.Field('C') == "40001" {
return true
}
return isNetworkError(err)
}