-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
82 lines (72 loc) · 1.54 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
package tcp
import (
"strings"
)
// Err represents a TCP error.
type Err interface {
error
// Recovered returns true if the error comes from a panic recovering.
Recovered() bool
}
// List of common errors
var (
// ErrRequest is returned if the request is invalid.
ErrRequest = NewError("invalid request")
)
// NewError returns a new Error based of the given cause.
func NewError(msg string, cause ...error) *Error {
if cause == nil {
return &Error{msg: msg}
}
return &Error{msg: msg, cause: cause[0]}
}
// Error represents a error message.
// It can wraps another error, its cause.
type Error struct {
msg string
cause error
recover bool
}
// Error implements the Err interface.
func (e *Error) Error() string {
const prefix = "tcp: "
if e.cause == nil {
return prefix + e.msg
}
return prefix + e.msg + ": " + e.cause.Error()
}
// Recovered implements the Err interface.
func (e *Error) Recovered() bool {
return e.recover
}
// Errors contains the list of errors occurred during the request.
type Errors []error
// Error implements the Err interface.
func (e Errors) Error() string {
var (
b strings.Builder
err error
)
for i, r := range e {
if i > 0 {
if _, err = b.WriteString(", "); err != nil {
return err.Error()
}
}
if _, err = b.WriteString(r.Error()); err != nil {
return err.Error()
}
}
return b.String()
}
// Recovered implements the Err interface.
func (e Errors) Recovered() (ok bool) {
var err Err
for _, r := range e {
err, ok = r.(Err)
if ok && err.Recovered() {
return
}
}
return
}