forked from Brodic/goots
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherror.go
241 lines (182 loc) · 4.79 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2014 The GiterLab Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// errors for ots2
package goots
import (
"fmt"
)
// 如果用户不希望使用panic模式,则设置此为false
var OTSErrorPanicMode bool = true // 默认开启panic模式
var ErrNonResponseBody = fmt.Errorf("response body not found")
var ErrReadResponse = fmt.Errorf("response body read fail")
type OTSError struct {
ClientError *OTSClientError
ServiceError *OTSServiceError
}
func (o OTSError) Set(format string, a ...interface{}) (e error) {
defer func() {
if OTSErrorPanicMode {
panic(e)
}
}()
e = fmt.Errorf(format, a...)
return e
}
func (o OTSError) Log(enable bool, format string, a ...interface{}) (e error) {
e = fmt.Errorf(format, a...)
// TODO:
// log to file
if enable {
}
return e
}
func (o *OTSError) Error() string {
var client_message string
var service_message string
if o.ClientError == nil {
client_message = "[C]-None"
} else {
client_message = "[C]-" + o.ClientError.Message
}
if o.ServiceError == nil {
service_message = "[S]-None"
} else {
service_message = "[S]-" + o.ServiceError.Code + " @ " + o.ServiceError.Message + " on " + o.ServiceError.RequestId
}
return client_message + " <--> " + service_message
}
func (o *OTSError) String() string {
var client_message string
var service_message string
if o.ClientError == nil {
client_message = "[C]-None"
} else {
client_message = o.ClientError.Message
}
if o.ServiceError == nil {
service_message = "[S]-None"
} else {
service_message = "[S]-" + o.ServiceError.Code + " @ " + o.ServiceError.Message + " on " + o.ServiceError.RequestId
}
return client_message + " <--> " + service_message
}
func (o *OTSError) SetClientError(client_err *OTSClientError) *OTSError {
o.ClientError = client_err
return o
}
func (o *OTSError) SetClientMessage(format string, a ...interface{}) *OTSError {
o.ClientError = new(OTSClientError)
o.ClientError.SetErrorMessage(format, a...)
return o
}
func (o *OTSError) SetServiceError(service_err *OTSServiceError) *OTSError {
o.ServiceError = service_err
return o
}
func (o *OTSError) SetServiceMessage(format string, a ...interface{}) *OTSError {
o.ServiceError = new(OTSServiceError)
o.ServiceError.SetErrorMessage(format, a...)
return o
}
type OTSClientError struct {
Message string
HttpStatus int
}
func (o OTSClientError) Set(format string, a ...interface{}) (e error) {
defer func() {
if OTSErrorPanicMode {
panic(e)
}
}()
e = fmt.Errorf(format, a...)
return e
}
func (o OTSClientError) Log(enable bool, format string, a ...interface{}) (e error) {
e = fmt.Errorf(format, a...)
// TODO:
// log to file
if enable {
}
return e
}
func (o *OTSClientError) Error() string {
return "[C]-" + o.Message
}
func (o *OTSClientError) String() string {
return "[C]-" + o.Message
}
func (o *OTSClientError) SetHttpStatus(status int) *OTSClientError {
o.HttpStatus = status
return o
}
func (o *OTSClientError) GetHttpStatus() int {
return o.HttpStatus
}
func (o *OTSClientError) SetErrorMessage(format string, a ...interface{}) *OTSClientError {
o.Message = fmt.Sprintf(format, a...)
return o
}
func (o *OTSClientError) GetErrorMessage() string {
return o.Message
}
type OTSServiceError struct {
HttpStatus int
Code string
Message string
RequestId string
Err error
}
func (o OTSServiceError) Set(format string, a ...interface{}) (e error) {
defer func() {
if OTSErrorPanicMode {
panic(e)
}
}()
e = fmt.Errorf(format, a...)
return e
}
func (o OTSServiceError) Log(enable bool, format string, a ...interface{}) (e error) {
e = fmt.Errorf(format, a...)
// TODO:
// log to file
if enable {
}
return e
}
func (o *OTSServiceError) Error() string {
return fmt.Sprintf("[S]-ErrorCode: %s, ErrorMessage: %s, RequestID: %s",
o.Code, o.Message, o.RequestId)
}
func (o *OTSServiceError) String() string {
return fmt.Sprintf("[S]-ErrorCode: %s, ErrorMessage: %s, RequestID: %s",
o.Code, o.Message, o.RequestId)
}
func (o *OTSServiceError) SetHttpStatus(status int) *OTSServiceError {
o.HttpStatus = status
return o
}
func (o *OTSServiceError) GetHttpStatus() int {
return o.HttpStatus
}
func (o *OTSServiceError) SetErrorCode(code string) *OTSServiceError {
o.Code = code
return o
}
func (o *OTSServiceError) GetErrorCode() string {
return o.Code
}
func (o *OTSServiceError) SetErrorMessage(format string, a ...interface{}) *OTSServiceError {
o.Message = fmt.Sprintf(format, a...)
return o
}
func (o *OTSServiceError) GetErrorMessage() string {
return o.Message
}
func (o *OTSServiceError) SetRequestId(request_id string) *OTSServiceError {
o.RequestId = request_id
return o
}
func (o *OTSServiceError) GetRequestId() string {
return o.RequestId
}