forked from uber/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
167 lines (132 loc) · 6.36 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tchannel
import (
"fmt"
"golang.org/x/net/context"
)
const (
// Message id for protocol level errors
invalidMessageID uint32 = 0xFFFFFFFF
)
// A SystemErrCode indicates how a caller should handle a system error returned from a peer
type SystemErrCode byte
//go:generate stringer -type=SystemErrCode
const (
// ErrCodeInvalid is an invalid error code, and should not be used
ErrCodeInvalid SystemErrCode = 0x00
// ErrCodeTimeout indicates the peer timed out. Callers can retry the request
// on another peer if the request is safe to retry.
ErrCodeTimeout SystemErrCode = 0x01
// ErrCodeCancelled indicates that the request was cancelled on the peer. Callers
// can retry the request on the same or another peer if the request is safe to retry
ErrCodeCancelled SystemErrCode = 0x02
// ErrCodeBusy indicates that the request was not dispatched because the peer
// was too busy to handle it. Callers can retry the request on another peer, and should
// reweight their connections to direct less traffic to this peer until it recovers.
ErrCodeBusy SystemErrCode = 0x03
// ErrCodeDeclined indicates that the request not dispatched because the peer
// declined to handle it, typically because the peer is not yet ready to handle it.
// Callers can retry the request on another peer, but should not reweight their connections
// and should continue to send traffic to this peer.
ErrCodeDeclined SystemErrCode = 0x04
// ErrCodeUnexpected indicates that the request failed for an unexpected reason, typically
// a crash or other unexpected handling. The request may have been processed before the failure;
// callers should retry the request on this or another peer only if the request is safe to retry
ErrCodeUnexpected SystemErrCode = 0x05
// ErrCodeBadRequest indicates that the request was malformed, and could not be processed.
// Callers should not bother to retry the request, as there is no chance it will be handled.
ErrCodeBadRequest SystemErrCode = 0x06
// ErrCodeNetwork indicates a network level error, such as a connection reset.
// Callers can retry the request if the request is safe to retry
ErrCodeNetwork SystemErrCode = 0x07
// ErrCodeProtocol indincates a fatal protocol error communicating with the peer. The connection
// will be terminated.
ErrCodeProtocol SystemErrCode = 0xFF
)
var (
// ErrServerBusy is a SystemError indicating the server is busy
ErrServerBusy = NewSystemError(ErrCodeBusy, "server busy")
// ErrRequestCancelled is a SystemError indicating the request has been cancelled on the peer
ErrRequestCancelled = NewSystemError(ErrCodeCancelled, "request cancelled")
// ErrTimeout is a SytemError indicating the request has timed out
ErrTimeout = NewSystemError(ErrCodeTimeout, "timeout")
// ErrTimeoutRequired is a SystemError indicating that timeouts must be specified.
ErrTimeoutRequired = NewSystemError(ErrCodeBadRequest, "timeout required")
// ErrChannelClosed is a SystemError indicating that the channel has been closed.
ErrChannelClosed = NewSystemError(ErrCodeDeclined, "closed channel")
// ErrMethodTooLarge is a SystemError indicating that the method is too large.
ErrMethodTooLarge = NewSystemError(ErrCodeProtocol, "method too large")
)
// A SystemError is a system-level error, containing an error code and message
// TODO(mmihic): Probably we want to hide this interface, and let application code
// just deal with standard raw errors.
type SystemError struct {
code SystemErrCode
msg string
wrapped error
}
// NewSystemError defines a new SystemError with a code and message
func NewSystemError(code SystemErrCode, msg string, args ...interface{}) error {
return SystemError{code: code, msg: fmt.Sprintf(msg, args...)}
}
// NewWrappedSystemError defines a new SystemError wrapping an existing error
func NewWrappedSystemError(code SystemErrCode, wrapped error) error {
if se, ok := wrapped.(SystemError); ok {
return se
}
return SystemError{code: code, msg: fmt.Sprintf("sys err %x: %s", code, wrapped.Error()), wrapped: wrapped}
}
// Error returns the code and message, conforming to the error interface
func (se SystemError) Error() string {
return fmt.Sprintf("tchannel error %v: %s", se.Code(), se.msg)
}
// Wrapped returns the wrapped error
func (se SystemError) Wrapped() error { return se.wrapped }
// Code returns the SystemError code, for sending to a peer
func (se SystemError) Code() SystemErrCode {
return se.code
}
// Message returns the SystemError message.
func (se SystemError) Message() string {
return se.msg
}
// GetContextError converts the context error to a tchannel error.
func GetContextError(err error) error {
if err == context.DeadlineExceeded {
return ErrTimeout
}
return err
}
// GetSystemErrorCode returns the code to report for the given error. If the error is a
// SystemError, we can get the code directly. Otherwise treat it as an unexpected error
func GetSystemErrorCode(err error) SystemErrCode {
if se, ok := err.(SystemError); ok {
return se.Code()
}
return ErrCodeUnexpected
}
// GetSystemErrorMessage returns the message to report for the given error. If the error is a
// SystemError, we can get the underlying message. Otherwise, use the Error() method.
func GetSystemErrorMessage(err error) string {
if se, ok := err.(SystemError); ok {
return se.Message()
}
return err.Error()
}