-
Notifications
You must be signed in to change notification settings - Fork 20
/
error.go
96 lines (78 loc) · 2.03 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
package nsq
import (
"bufio"
"io"
"strings"
"github.com/pkg/errors"
)
// Error is a frame type representing error responses to commands.
type Error string
const (
ErrInvalid Error = "E_INVALID"
ErrBadBody Error = "E_BAD_BODY"
ErrBadTopic Error = "E_BAD_TOPIC"
ErrBadChannel Error = "E_BAD_CHANNEL"
ErrBadMessage Error = "E_BAD_MESSAGE"
ErrPubFailed Error = "E_PUB_FAILED"
ErrMPubFailed Error = "E_MPUB_FAILED"
ErrFinFailed Error = "E_FIN_FAILED"
ErrReqFailed Error = "E_REQ_FAILED"
ErrTouchFailed Error = "E_TOUCH_FAILED"
ErrAuthFailed Error = "E_AUTH_FAILED"
ErrUnauthorized Error = "E_UNAUTHORIZED"
)
// String returns the error as a string, satisfies the error interface.
func (e Error) Error() string {
return string(e)
}
// String returns the error as a string.
func (e Error) String() string {
return string(e)
}
// FrameType returns FrameTypeError, satisfies the Frame interface.
func (e Error) FrameType() FrameType {
return FrameTypeError
}
// Write serializes the frame to the given buffered output, satisfies the Frame
// interface.
func (e Error) Write(w *bufio.Writer) (err error) {
if err = writeFrameHeader(w, FrameTypeError, len(e)); err != nil {
err = errors.WithMessage(err, "writing error message")
return
}
if _, err = w.WriteString(string(e)); err != nil {
err = errors.Wrap(err, "writing error message")
return
}
return
}
func readError(n int, r *bufio.Reader) (e Error, err error) {
data := make([]byte, n)
if _, err = io.ReadFull(r, data); err != nil {
err = errors.Wrap(err, "reading error message")
return
}
e = Error(data)
return
}
type errorList []error
func (e errorList) Error() string {
list := make([]string, 0, len(e))
for _, err := range e {
list = append(list, err.Error())
}
return strings.Join(list, "; ")
}
func appendError(errList error, err error) error {
if err == nil {
return errList
}
switch e := errList.(type) {
case errorList:
return append(e, err)
case error:
return errorList{e, err}
default:
return errorList{err}
}
}