forked from blind-oracle/psql-streamer
/
error.go
78 lines (63 loc) · 1.56 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
package common
import (
"database/sql"
"fmt"
)
// These two are common
type temporary interface{ Temporary() bool }
type timeout interface{ Timeout() bool }
// This one is used in pq driver
type fatal interface{ Fatal() bool }
// IsErrorTemporary tries to guess if an error is temporary or fatal
// If unsure - report as temporary
func IsErrorTemporary(err error) bool {
// Check specific error types first
if err == sql.ErrNoRows {
return false
}
// Then try common interfaces
if e, ok := err.(temporary); ok {
return e.Temporary()
}
if e, ok := err.(timeout); ok {
return e.Timeout()
}
if e, ok := err.(fatal); ok {
return !e.Fatal()
}
return true
}
// Error is a common error type with permanent flag
type Error struct {
message string
permanent bool
}
func (e Error) Error() string {
return e.message
}
// Temporary returns false if the error is permanent
func (e Error) Temporary() bool {
return !e.permanent
}
func errGeneric(msg string, f ...interface{}) Error {
return Error{
message: fmt.Sprintf(msg, f...),
}
}
// ErrorInheritf creates a new error.
// It tries to set the permanent flag based on the provided error.
func ErrorInheritf(err error, msg string, f ...interface{}) error {
e := errGeneric(msg, f...)
e.permanent = !IsErrorTemporary(err)
return e
}
// ErrorPermf returns permanent error
func ErrorPermf(msg string, f ...interface{}) error {
e := errGeneric(msg, f...)
e.permanent = true
return e
}
// ErrorTempf returns temporary error
func ErrorTempf(msg string, f ...interface{}) error {
return errGeneric(msg, f...)
}