-
Notifications
You must be signed in to change notification settings - Fork 113
/
errtypes.go
264 lines (205 loc) · 9.08 KB
/
errtypes.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
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
// Package errtypes contains definitions for common errors.
// It would have nice to call this package errors, err or error
// but errors clashes with github.com/pkg/errors, err is used for any error variable
// and error is a reserved word :)
package errtypes
import (
"strings"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
)
// NotFound is the error to use when a something is not found.
type NotFound string
func (e NotFound) Error() string { return "error: not found: " + string(e) }
// IsNotFound implements the IsNotFound interface.
func (e NotFound) IsNotFound() {}
// InternalError is the error to use when we really don't know what happened. Use with care
type InternalError string
func (e InternalError) Error() string { return "internal error: " + string(e) }
// IsInternalError implements the IsInternalError interface.
func (e InternalError) IsInternalError() {}
// PermissionDenied is the error to use when a resource cannot be access because of missing permissions.
type PermissionDenied string
func (e PermissionDenied) Error() string { return "error: permission denied: " + string(e) }
// IsPermissionDenied implements the IsPermissionDenied interface.
func (e PermissionDenied) IsPermissionDenied() {}
// Locked is the error to use when a resource cannot be modified because of a lock.
type Locked string
func (e Locked) Error() string { return "error: locked by " + string(e) }
// LockID returns the lock ID that caused this error
func (e Locked) LockID() string {
return string(e)
}
// IsLocked implements the IsLocked interface.
func (e Locked) IsLocked() {}
// PreconditionFailed is the error to use when a request fails because a requested etag or lock ID mismatches.
type PreconditionFailed string
func (e PreconditionFailed) Error() string { return "error: precondition failed: " + string(e) }
// IsPreconditionFailed implements the IsPreconditionFailed interface.
func (e PreconditionFailed) IsPreconditionFailed() {}
// AlreadyExists is the error to use when a resource something is not found.
type AlreadyExists string
func (e AlreadyExists) Error() string { return "error: already exists: " + string(e) }
// IsAlreadyExists implements the IsAlreadyExists interface.
func (e AlreadyExists) IsAlreadyExists() {}
// UserRequired represents an error when a resource is not found.
type UserRequired string
func (e UserRequired) Error() string { return "error: user required: " + string(e) }
// IsUserRequired implements the IsUserRequired interface.
func (e UserRequired) IsUserRequired() {}
// InvalidCredentials is the error to use when receiving invalid credentials.
type InvalidCredentials string
func (e InvalidCredentials) Error() string { return "error: invalid credentials: " + string(e) }
// IsInvalidCredentials implements the IsInvalidCredentials interface.
func (e InvalidCredentials) IsInvalidCredentials() {}
// NotSupported is the error to use when an action is not supported.
type NotSupported string
func (e NotSupported) Error() string { return "error: not supported: " + string(e) }
// IsNotSupported implements the IsNotSupported interface.
func (e NotSupported) IsNotSupported() {}
// PartialContent is the error to use when the client request has partial data.
type PartialContent string
func (e PartialContent) Error() string { return "error: partial content: " + string(e) }
// IsPartialContent implements the IsPartialContent interface.
func (e PartialContent) IsPartialContent() {}
// BadRequest is the error to use when the server cannot or will not process the request (due to a client error). Reauthenticating won't help.
type BadRequest string
func (e BadRequest) Error() string { return "error: bad request: " + string(e) }
// IsBadRequest implements the IsBadRequest interface.
func (e BadRequest) IsBadRequest() {}
// ChecksumMismatch is the error to use when the sent hash does not match the calculated hash.
type ChecksumMismatch string
func (e ChecksumMismatch) Error() string { return "error: checksum mismatch: " + string(e) }
// IsChecksumMismatch implements the IsChecksumMismatch interface.
func (e ChecksumMismatch) IsChecksumMismatch() {}
// StatusChecksumMismatch 419 is an unofficial http status code in an unassigned range that is used for checksum mismatches
// Proposed by https://stackoverflow.com/a/35665694
// Official HTTP status code registry: https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
// Note: TUS uses unassigned 460 Checksum-Mismatch
// RFC proposal for checksum digest uses a `Want-Digest` header: https://tools.ietf.org/html/rfc3230
// oc clienst issue: https://github.com/owncloud/core/issues/22711
const StatusChecksumMismatch = 419
// InsufficientStorage is the error to use when there is insufficient storage.
type InsufficientStorage string
func (e InsufficientStorage) Error() string { return "error: insufficient storage: " + string(e) }
// IsInsufficientStorage implements the IsInsufficientStorage interface.
func (e InsufficientStorage) IsInsufficientStorage() {}
// StatusInssufficientStorage 507 is an official http status code to indicate that there is insufficient storage
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/507
const StatusInssufficientStorage = 507
// IsNotFound is the interface to implement
// to specify that an a resource is not found.
type IsNotFound interface {
IsNotFound()
}
// IsAlreadyExists is the interface to implement
// to specify that a resource already exists.
type IsAlreadyExists interface {
IsAlreadyExists()
}
// IsInternalError is the interface to implement
// to specify that there was some internal error
type IsInternalError interface {
IsInternalError()
}
// IsUserRequired is the interface to implement
// to specify that a user is required.
type IsUserRequired interface {
IsUserRequired()
}
// IsInvalidCredentials is the interface to implement
// to specify that credentials were wrong.
type IsInvalidCredentials interface {
IsInvalidCredentials()
}
// IsNotSupported is the interface to implement
// to specify that an action is not supported.
type IsNotSupported interface {
IsNotSupported()
}
// IsPermissionDenied is the interface to implement
// to specify that an action is denied.
type IsPermissionDenied interface {
IsPermissionDenied()
}
// IsLocked is the interface to implement
// to specify that an resource is locked.
type IsLocked interface {
IsLocked()
}
// IsPreconditionFailed is the interface to implement
// to specify that a precondition failed.
type IsPreconditionFailed interface {
IsPreconditionFailed()
}
// IsPartialContent is the interface to implement
// to specify that the client request has partial data.
type IsPartialContent interface {
IsPartialContent()
}
// IsBadRequest is the interface to implement
// to specify that the server cannot or will not process the request.
type IsBadRequest interface {
IsBadRequest()
}
// IsChecksumMismatch is the interface to implement
// to specify that a checksum does not match.
type IsChecksumMismatch interface {
IsChecksumMismatch()
}
// IsInsufficientStorage is the interface to implement
// to specify that there is insufficient storage.
type IsInsufficientStorage interface {
IsInsufficientStorage()
}
// NewErrtypeFromStatus maps an rpc status to an errtype
func NewErrtypeFromStatus(status *rpc.Status) error {
switch status.Code {
case rpc.Code_CODE_OK:
return nil
case rpc.Code_CODE_NOT_FOUND:
return NotFound(status.Message)
case rpc.Code_CODE_ALREADY_EXISTS:
return AlreadyExists(status.Message)
// case rpc.Code_CODE_FAILED_PRECONDITION: ?
// return UserRequired(status.Message)
// case rpc.Code_CODE_PERMISSION_DENIED: ?
// IsInvalidCredentials
case rpc.Code_CODE_UNIMPLEMENTED:
return NotSupported(status.Message)
case rpc.Code_CODE_PERMISSION_DENIED:
// FIXME add locked status!
if strings.HasPrefix(status.Message, "set lock: error: locked by ") {
return Locked(strings.TrimPrefix(status.Message, "set lock: error: locked by "))
}
return PermissionDenied(status.Message)
// case rpc.Code_CODE_LOCKED:
// return Locked(status.Message)
// case rpc.Code_CODE_DATA_LOSS: ?
// IsPartialContent
case rpc.Code_CODE_FAILED_PRECONDITION:
return PreconditionFailed(status.Message)
case rpc.Code_CODE_INSUFFICIENT_STORAGE:
return InsufficientStorage(status.Message)
case rpc.Code_CODE_INVALID_ARGUMENT, rpc.Code_CODE_OUT_OF_RANGE:
return BadRequest(status.Message)
default:
return InternalError(status.Message)
}
}