forked from inconshreveable/muxado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
77 lines (66 loc) · 1.71 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
package muxado
import "errors"
import "github.com/inconshreveable/muxado/frame"
// ErrorCode is a 32-bit integer indicating the type of an error condition
type ErrorCode uint32
const (
NoError ErrorCode = iota
ProtocolError
InternalError
FlowControlError
StreamClosed
StreamRefused
StreamCancelled
StreamReset
FrameSizeError
AcceptQueueFull
EnhanceYourCalm
RemoteGoneAway
StreamsExhausted
WriteTimeout
SessionClosed
PeerEOF
ErrorUnknown ErrorCode = 0xFF
)
var (
remoteGoneAway = newErr(RemoteGoneAway, errors.New("remote gone away"))
streamsExhausted = newErr(StreamsExhausted, errors.New("streams exhuastated"))
streamClosed = newErr(StreamClosed, errors.New("stream closed"))
writeTimeout = newErr(WriteTimeout, errors.New("write timed out"))
flowControlViolated = newErr(FlowControlError, errors.New("flow control violated"))
sessionClosed = newErr(SessionClosed, errors.New("session closed"))
eofPeer = newErr(PeerEOF, errors.New("read EOF from remote peer"))
)
func fromFrameError(err error) error {
if e, ok := err.(*frame.Error); ok {
switch e.Type() {
case frame.ErrorFrameSize:
return &muxadoError{FrameSizeError, err}
case frame.ErrorProtocol, frame.ErrorProtocolStream:
return &muxadoError{ProtocolError, err}
}
}
return err
}
type muxadoError struct {
ErrorCode
error
}
func (e *muxadoError) Error() string {
if e.error != nil {
return e.error.Error()
}
return "<nil>"
}
func newErr(code ErrorCode, err error) error {
return &muxadoError{code, err}
}
func GetError(err error) (ErrorCode, error) {
if err == nil {
return NoError, nil
}
if e, ok := err.(*muxadoError); ok {
return e.ErrorCode, e.error
}
return ErrorUnknown, err
}