-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
107 lines (93 loc) · 1.71 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package pq
import (
"database/sql/driver"
"fmt"
"io"
"net"
"runtime"
)
const (
Efatal = "FATAL"
Epanic = "PANIC"
Ewarning = "WARNING"
Enotice = "NOTICE"
Edebug = "DEBUG"
Einfo = "INFO"
Elog = "LOG"
)
type Error error
type PGError interface {
Error() string
Fatal() bool
Get(k byte) (v string)
}
type pgError struct {
c map[byte]string
}
func parseError(r *readBuf) *pgError {
err := &pgError{make(map[byte]string)}
for t := r.byte(); t != 0; t = r.byte() {
err.c[t] = r.string()
}
return err
}
func (err *pgError) Get(k byte) (v string) {
return err.c[k]
}
func (err *pgError) Fatal() bool {
return err.Get('S') == Efatal
}
func (err *pgError) Error() string {
var s string
for k, v := range err.c {
s += fmt.Sprintf(" %c:%q", k, v)
}
return "pq: " + s[1:]
}
func errorf(s string, args ...interface{}) {
panic(Error(fmt.Errorf("pq: %s", fmt.Sprintf(s, args...))))
}
type SimplePGError struct {
pgError
}
func (err *SimplePGError) Error() string {
return "pq: " + err.Get('M')
}
func errRecoverWithPGReason(err *error) {
e := recover()
switch v := e.(type) {
case nil:
// Do nothing
case *pgError:
// Return a SimplePGError in place
*err = &SimplePGError{*v}
default:
// Otherwise re-panic
panic(e)
}
}
func errRecover(err *error) {
e := recover()
switch v := e.(type) {
case nil:
// Do nothing
case runtime.Error:
panic(v)
case *pgError:
if v.Fatal() {
*err = driver.ErrBadConn
} else {
*err = v
}
case *net.OpError:
*err = driver.ErrBadConn
case error:
if v == io.EOF || v.(error).Error() == "remote error: handshake failure" {
*err = driver.ErrBadConn
} else {
*err = v
}
default:
panic(fmt.Sprintf("unknown error: %#v", e))
}
}