-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
server_errors.go
365 lines (320 loc) · 11 KB
/
server_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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package kbfsblock
import (
"errors"
"fmt"
"strconv"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
)
const (
// StatusCodeServerError is the error code for a generic block server error.
StatusCodeServerError = 2700
// StatusCodeServerErrorBadRequest is the error code for a generic client error.
StatusCodeServerErrorBadRequest = 2701
// StatusCodeServerErrorUnauthorized is the error code for when the session has not been validated
StatusCodeServerErrorUnauthorized = 2702
// StatusCodeServerErrorOverQuota is the error code for when the user has exceeded his quota
StatusCodeServerErrorOverQuota = 2703
// StatusCodeServerErrorBlockNonExistent is the error code for when bserver cannot find a block
StatusCodeServerErrorBlockNonExistent = 2704
// StatusCodeServerErrorBlockArchived is the error code for a block has been archived
StatusCodeServerErrorBlockArchived = 2705
// StatusCodeServerErrorNoPermission is the error code for when there's no permission
StatusCodeServerErrorNoPermission = 2706
// StatusCodeServerErrorBlockDeleted is the error code for a block has been deleted
StatusCodeServerErrorBlockDeleted = 2707
// StatusCodeServerErrorNonceNonExistent is the error code when a nonce cannot be found
StatusCodeServerErrorNonceNonExistent = 2708
// StatusCodeServerErrorMaxRefExceeded is the error code to indicate there are too many refs to a block
StatusCodeServerErrorMaxRefExceeded = 2709
// StatusCodeServerErrorThrottle is the error code to indicate the client should initiate backoff.
StatusCodeServerErrorThrottle = 2799
)
// ServerError is a generic bserver-side error.
type ServerError struct {
Msg string
}
// ToStatus implements the ExportableError interface for ServerError.
func (e ServerError) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerError
s.Name = "SERVER_ERROR"
s.Desc = e.Msg
return
}
// Error implements the Error interface for ServerError.
func (e ServerError) Error() string {
return "ServerError{" + e.Msg + "}"
}
// ServerErrorBadRequest is a generic client-side error.
type ServerErrorBadRequest struct {
Msg string
}
// ToStatus implements the ExportableError interface for ServerError.
func (e ServerErrorBadRequest) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerErrorBadRequest
s.Name = "BAD_REQUEST"
s.Desc = e.Msg
return
}
// Error implements the Error interface for ServerError.
func (e ServerErrorBadRequest) Error() string {
if e.Msg == "" {
return "Server: bad client request"
}
return "ServerErrorBadRequest{" + e.Msg + "}"
}
// ServerErrorUnauthorized is a generic client-side error.
type ServerErrorUnauthorized struct {
Msg string
}
// ToStatus implements the ExportableError interface for ServerErrorUnauthorized.
func (e ServerErrorUnauthorized) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerErrorUnauthorized
s.Name = "SESSION_UNAUTHORIZED"
s.Desc = e.Msg
return
}
// Error implements the Error interface for ServerErrorUnauthorized.
func (e ServerErrorUnauthorized) Error() string {
if e.Msg == "" {
return "Server: session not validated"
}
return "ServerErrorUnauthorized{" + e.Msg + "}"
}
// ServerErrorOverQuota is returned when a user is over quota.
type ServerErrorOverQuota struct {
Msg string
// Usage indicates the current usage
Usage int64
// Limit indicates the current quota limit
Limit int64
// Throttled indicates if request has not been completed due to server throttle
Throttled bool
}
// ToStatus implements the ExportableError interface for ServerErrorOverQuota.
func (e ServerErrorOverQuota) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerErrorOverQuota
s.Name = "QUOTA_EXCEEDED"
s.Desc = e.Msg
s.Fields = append(s.Fields, keybase1.StringKVPair{
Key: "QUOTA_USAGE",
Value: strconv.FormatInt(e.Usage, 10),
})
s.Fields = append(s.Fields, keybase1.StringKVPair{
Key: "QUOTA_LIMIT",
Value: strconv.FormatInt(e.Limit, 10),
})
s.Fields = append(s.Fields, keybase1.StringKVPair{
Key: "QUOTA_THROTTLE",
Value: strconv.FormatBool(e.Throttled),
})
return
}
// Error implements the Error interface for ServerErrorOverQuota.
func (e ServerErrorOverQuota) Error() string {
return fmt.Sprintf(
"ServerErrorOverQuota{Msg: %q, Usage: %d, Limit: %d, Throttled: %t}",
e.Msg, e.Usage, e.Limit, e.Throttled)
}
// ServerErrorBlockNonExistent is an exportable error from bserver
type ServerErrorBlockNonExistent struct {
Msg string
}
// ToStatus implements the ExportableError interface for ServerErrorBlockNonExistent
func (e ServerErrorBlockNonExistent) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerErrorBlockNonExistent
s.Name = "BLOCK_NONEXISTENT"
s.Desc = e.Msg
return
}
// Error implements the Error interface for ServerErrorBlockNonExistent.
func (e ServerErrorBlockNonExistent) Error() string {
if e.Msg == "" {
return "Server: block does not exist"
}
return "ServerErrorBlockNonExistent{" + e.Msg + "}"
}
// ServerErrorBlockArchived is an exportable error from bserver
type ServerErrorBlockArchived struct {
Msg string
}
// ToStatus implements the ExportableError interface for ServerErrorBlockArchived
func (e ServerErrorBlockArchived) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerErrorBlockArchived
s.Name = "BLOCK_ARCHIVED"
s.Desc = e.Msg
return
}
// Error implements the Error interface for ServerErrorBlockArchived.
func (e ServerErrorBlockArchived) Error() string {
if e.Msg == "" {
return "Server: block is archived"
}
return "ServerErrorBlockArchived{" + e.Msg + "}"
}
// ServerErrorBlockDeleted is an exportable error from bserver
type ServerErrorBlockDeleted struct {
Msg string
}
// ToStatus implements the ExportableError interface for ServerErrorBlockDeleted
func (e ServerErrorBlockDeleted) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerErrorBlockDeleted
s.Name = "BLOCK_DELETED"
s.Desc = e.Msg
return
}
// Error implements the Error interface for ServerErrorBlockDeleted
func (e ServerErrorBlockDeleted) Error() string {
if e.Msg == "" {
return "Server: block is deleted"
}
return "ServerErrorBlockDeleted{" + e.Msg + "}"
}
// ServerErrorNoPermission is an exportable error from bserver
type ServerErrorNoPermission struct {
Msg string
}
// ToStatus implements the ExportableError interface for ServerErrorBlockArchived
func (e ServerErrorNoPermission) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerErrorNoPermission
s.Name = "NO_PERMISSION"
s.Desc = e.Msg
return
}
// Error implements the Error interface for ServerErrorNoPermission.
func (e ServerErrorNoPermission) Error() string {
if e.Msg == "" {
return "Server: permission denied"
}
return "ServerErrorNoPermission{" + e.Msg + "}"
}
// ServerErrorNonceNonExistent is an exportable error from bserver
type ServerErrorNonceNonExistent struct {
Msg string
}
// ToStatus implements the ExportableError interface for ServerErrorNonceNonExistent
func (e ServerErrorNonceNonExistent) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerErrorNonceNonExistent
s.Name = "BLOCK_NONCENONEXISTENT"
s.Desc = e.Msg
return
}
// Error implements the Error interface for ServerErrornonceNonExistent.
func (e ServerErrorNonceNonExistent) Error() string {
if e.Msg == "" {
return "Server: reference nonce does not exist"
}
return "ServerErrorNonceNonExistent{" + e.Msg + "}"
}
// ServerErrorMaxRefExceeded is an exportable error from bserver
type ServerErrorMaxRefExceeded struct {
Msg string
}
// ToStatus implements the ExportableError interface for ServerErrorMaxRefExceeded
func (e ServerErrorMaxRefExceeded) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerErrorMaxRefExceeded
s.Name = "BLOCK_MAXREFEXCEEDED"
s.Desc = e.Msg
return
}
// Error implements the Error interface for ServerErrorMaxRefExceeded
func (e ServerErrorMaxRefExceeded) Error() string {
if e.Msg == "" {
return "Server: maximum allowed number of references exceeded"
}
return "ServerErrorMaxRefExceeded{" + e.Msg + "}"
}
// ServerErrorThrottle is returned when the server wants the client to backoff.
type ServerErrorThrottle struct {
Msg string
}
// Error implements the Error interface for ServerErrorThrottle.
func (e ServerErrorThrottle) Error() string {
return "ServerErrorThrottle{" + e.Msg + "}"
}
// ToStatus implements the ExportableError interface for ServerErrorThrottle.
func (e ServerErrorThrottle) ToStatus() (s keybase1.Status) {
s.Code = StatusCodeServerErrorThrottle
s.Name = "ERROR_THROTTLE"
s.Desc = e.Msg
return
}
// ServerErrorUnwrapper unwraps errors from a remote block server.
type ServerErrorUnwrapper struct{}
var _ rpc.ErrorUnwrapper = ServerErrorUnwrapper{}
// MakeArg implements rpc.ErrorUnwrapper.
func (eu ServerErrorUnwrapper) MakeArg() interface{} {
return &keybase1.Status{}
}
// UnwrapError implements rpc.ErrorUnwrapper.
func (eu ServerErrorUnwrapper) UnwrapError(arg interface{}) (appError error, dispatchError error) {
s, ok := arg.(*keybase1.Status)
if !ok {
return nil, errors.New("Error converting arg to keybase1.Status object in ServerErrorUnwrapper.UnwrapError")
}
if s == nil || s.Code == 0 {
return nil, nil
}
switch s.Code {
case StatusCodeServerError:
appError = ServerError{Msg: s.Desc}
case StatusCodeServerErrorBadRequest:
appError = ServerErrorBadRequest{Msg: s.Desc}
case StatusCodeServerErrorUnauthorized:
appError = ServerErrorUnauthorized{Msg: s.Desc}
case StatusCodeServerErrorOverQuota:
quotaErr := ServerErrorOverQuota{Msg: s.Desc}
for _, f := range s.Fields {
switch {
case f.Key == "QUOTA_USAGE":
quotaErr.Usage, _ = strconv.ParseInt(f.Value, 10, 64)
case f.Key == "QUOTA_LIMIT":
quotaErr.Limit, _ = strconv.ParseInt(f.Value, 10, 64)
case f.Key == "QUOTA_THROTTLE":
quotaErr.Throttled, _ = strconv.ParseBool(f.Value)
}
}
appError = quotaErr
case StatusCodeServerErrorBlockNonExistent:
appError = ServerErrorBlockNonExistent{Msg: s.Desc}
case StatusCodeServerErrorBlockArchived:
appError = ServerErrorBlockArchived{Msg: s.Desc}
case StatusCodeServerErrorNoPermission:
appError = ServerErrorNoPermission{Msg: s.Desc}
case StatusCodeServerErrorThrottle:
appError = ServerErrorThrottle{Msg: s.Desc}
case StatusCodeServerErrorBlockDeleted:
appError = ServerErrorBlockDeleted{Msg: s.Desc}
case StatusCodeServerErrorNonceNonExistent:
appError = ServerErrorNonceNonExistent{Msg: s.Desc}
case StatusCodeServerErrorMaxRefExceeded:
appError = ServerErrorMaxRefExceeded{Msg: s.Desc}
default:
ase := libkb.AppStatusError{
Code: s.Code,
Name: s.Name,
Desc: s.Desc,
Fields: make(map[string]string),
}
for _, f := range s.Fields {
ase.Fields[f.Key] = f.Value
}
appError = ase
}
return appError, nil
}
// IsThrottleError returns whether or not the given error signals
// throttling.
func IsThrottleError(err error) bool {
if _, ok := err.(ServerErrorThrottle); ok {
return true
}
if quotaErr, ok := err.(ServerErrorOverQuota); ok && quotaErr.Throttled {
return true
}
return false
}