-
Notifications
You must be signed in to change notification settings - Fork 410
/
status.go
163 lines (152 loc) · 5.17 KB
/
status.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
package erpc
import (
"github.com/andeya/goutil/status"
)
// Status a handling status with code, msg, cause and stack.
type Status = status.Status
var (
// NewStatus creates a message status with code, msg and cause.
// NOTE:
// code=0 means no error
// TYPE:
// func NewStatus(code int32, msg string, cause interface{}) *Status
NewStatus = status.New
// NewStatusWithStack creates a message status with code, msg and cause and stack.
// NOTE:
// code=0 means no error
// TYPE:
// func NewStatusWithStack(code int32, msg string, cause interface{}) *Status
NewStatusWithStack = status.NewWithStack
// NewStatusFromQuery parses the query bytes to a status object.
// TYPE:
// func NewStatusFromQuery(b []byte, tagStack bool) *Status
NewStatusFromQuery = status.FromQuery
// CheckStatus if err!=nil, create a status with stack, and panic.
// NOTE:
// If err!=nil and msg=="", error text is set to msg
// TYPE:
// func Check(err error, code int32, msg string, whenError ...func())
CheckStatus = status.Check
// ThrowStatus creates a status with stack, and panic.
// TYPE:
// func Throw(code int32, msg string, cause interface{})
ThrowStatus = status.Throw
// PanicStatus panic with stack trace.
// TYPE:
// func Panic(stat *Status)
PanicStatus = status.Panic
// CatchStatus recovers the panic and returns status.
// NOTE:
// Set `realStat` to true if a `Status` type is recovered
// Example:
// var stat *Status
// defer Catch(&stat)
// TYPE:
// func Catch(statPtr **Status, realStat ...*bool)
CatchStatus = status.Catch
)
// NewStatusByCodeText creates a message status with code, msg, cause or stack.
// NOTE:
//
// The msg comes from the CodeText(code) value.
func NewStatusByCodeText(code int32, cause interface{}, tagStack bool) *Status {
stat := NewStatus(code, CodeText(code), cause)
if tagStack {
stat.TagStack(1)
}
return stat
}
// Internal Framework Status code.
// NOTE: Recommended custom code is greater than 1000.
//
// unknown error code: -1.
// sender peer error code range: [100,199].
// message handling error code range: [400,499].
// receiver peer error code range: [500,599].
const (
CodeUnknownError int32 = -1
CodeOK int32 = 0 // nil error (ok)
CodeNoError int32 = CodeOK // nil error (ok)
CodeInvalidOp int32 = 1
CodeWrongConn int32 = 100
CodeConnClosed int32 = 102
CodeWriteFailed int32 = 104
CodeDialFailed int32 = 105
CodeBadMessage int32 = 400
CodeUnauthorized int32 = 401
CodeNotFound int32 = 404
CodeMtypeNotAllowed int32 = 405
CodeHandleTimeout int32 = 408
CodeInternalServerError int32 = 500
CodeBadGateway int32 = 502
// CodeConflict int32 = 409
// CodeUnsupportedTx int32 = 410
// CodeUnsupportedCodecType int32 = 415
// CodeServiceUnavailable int32 = 503
// CodeGatewayTimeout int32 = 504
// CodeVariantAlsoNegotiates int32 = 506
// CodeInsufficientStorage int32 = 507
// CodeLoopDetected int32 = 508
// CodeNotExtended int32 = 510
// CodeNetworkAuthenticationRequired int32 = 511
)
// CodeText returns the reply error code text.
// If the type is undefined returns 'Unknown Error'.
func CodeText(statCode int32) string {
switch statCode {
case CodeNoError:
return ""
case CodeInvalidOp:
return "Invalid Operation"
case CodeBadMessage:
return "Bad Message"
case CodeUnauthorized:
return "Unauthorized"
case CodeDialFailed:
return "Dial Failed"
case CodeWrongConn:
return "Wrong Connection"
case CodeConnClosed:
return "Connection Closed"
case CodeWriteFailed:
return "Write Failed"
case CodeNotFound:
return "Not Found"
case CodeHandleTimeout:
return "Handle Timeout"
case CodeMtypeNotAllowed:
return "Message Type Not Allowed"
case CodeInternalServerError:
return "Internal Server Error"
case CodeBadGateway:
return "Bad Gateway"
case CodeUnknownError:
fallthrough
default:
return "Unknown Error"
}
}
// Internal Framework Status string.
var (
statInvalidOpError = NewStatus(CodeInvalidOp, CodeText(CodeInvalidOp), "")
statUnknownError = NewStatus(CodeUnknownError, CodeText(CodeUnknownError), "")
statDialFailed = NewStatus(CodeDialFailed, CodeText(CodeDialFailed), "")
statConnClosed = NewStatus(CodeConnClosed, CodeText(CodeConnClosed), "")
statWriteFailed = NewStatus(CodeWriteFailed, CodeText(CodeWriteFailed), "")
statBadMessage = NewStatus(CodeBadMessage, CodeText(CodeBadMessage), "")
statNotFound = NewStatus(CodeNotFound, CodeText(CodeNotFound), "")
statCodeMtypeNotAllowed = NewStatus(CodeMtypeNotAllowed, CodeText(CodeMtypeNotAllowed), "")
statHandleTimeout = NewStatus(CodeHandleTimeout, CodeText(CodeHandleTimeout), "")
statInternalServerError = NewStatus(CodeInternalServerError, CodeText(CodeInternalServerError), "")
)
// IsConnError determines whether the status is a connection error.
func IsConnError(stat *Status) bool {
if stat == nil {
return false
}
code := stat.Code()
if code == CodeDialFailed || code == CodeConnClosed {
return true
}
return false
}