-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
47 lines (40 loc) · 981 Bytes
/
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
package hermes
import "github.com/jackc/pgconn"
// PostgreSQL disconnect errors - https://www.postgresql.org/docs/current/errcodes-appendix.html
const (
OperatorIntervention = "57000"
QueryCanceled = "57014"
AdminShutdown = "57P01"
CrashShutdown = "57P02"
CannotConnectNow = "57P03"
DatabaseDropped = "57P04"
IdleSessionTimeout = "57P05"
)
var (
// Disconnects is the list of PostgreSQL error codes that indicate the connection failed.
Disconnects = []string{
OperatorIntervention,
QueryCanceled,
AdminShutdown,
CrashShutdown,
CannotConnectNow,
DatabaseDropped,
IdleSessionTimeout,
}
)
// IsDisconnected returns true if the error is a PostgreSQL disconnect error (SQLSTATE 57P01).
func IsDisconnected(err error) bool {
if err == nil {
return false
}
pgErr, ok := err.(*pgconn.PgError)
if !ok {
return false
}
for _, code := range Disconnects {
if pgErr.Code == code {
return true
}
}
return false
}