-
Notifications
You must be signed in to change notification settings - Fork 562
/
errors.go
384 lines (353 loc) · 10.6 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package daemon
import (
"errors"
"fmt"
"net"
"net/http"
"github.com/snapcore/snapd/arch"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/overlord/servicestate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/store"
)
// apiError represents an error meant for returning to the client.
// It can serialize itself to our standard JSON response format.
type apiError struct {
// Status is the error HTTP status code.
Status int
Message string
// Kind is the error kind. See client/errors.go
Kind client.ErrorKind
Value errorValue
}
func (ae *apiError) Error() string {
kindOrStatus := "api"
if ae.Kind != "" {
kindOrStatus = fmt.Sprintf("api: %s", ae.Kind)
} else if ae.Status != 400 {
kindOrStatus = fmt.Sprintf("api %d", ae.Status)
}
return fmt.Sprintf("%s (%s)", ae.Message, kindOrStatus)
}
func (ae *apiError) JSON() *respJSON {
return &respJSON{
Status: ae.Status,
Type: ResponseTypeError,
Result: &errorResult{
Message: ae.Message,
Kind: ae.Kind,
Value: ae.Value,
},
}
}
func (ae *apiError) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ae.JSON().ServeHTTP(w, r)
}
// check it implements StructuredResponse
var _ StructuredResponse = (*apiError)(nil)
type errorValue interface{}
type errorResult struct {
Message string `json:"message"` // note no omitempty
// Kind is the error kind. See client/errors.go
Kind client.ErrorKind `json:"kind,omitempty"`
Value errorValue `json:"value,omitempty"`
}
// errorResponder is a callable that produces an error Response.
// e.g., InternalError("something broke: %v", err), etc.
type errorResponder func(string, ...interface{}) *apiError
// makeErrorResponder builds an errorResponder from the given error status.
func makeErrorResponder(status int) errorResponder {
return func(format string, v ...interface{}) *apiError {
var msg string
if len(v) == 0 {
msg = format
} else {
msg = fmt.Sprintf(format, v...)
}
var kind client.ErrorKind
if status == 401 || status == 403 {
kind = client.ErrorKindLoginRequired
}
return &apiError{
Status: status,
Message: msg,
Kind: kind,
}
}
}
// standard error responses
var (
Unauthorized = makeErrorResponder(401)
NotFound = makeErrorResponder(404)
BadRequest = makeErrorResponder(400)
MethodNotAllowed = makeErrorResponder(405)
InternalError = makeErrorResponder(500)
NotImplemented = makeErrorResponder(501)
Forbidden = makeErrorResponder(403)
Conflict = makeErrorResponder(409)
)
// BadQuery is an error responder used when a bad query was
// provided to the store.
func BadQuery() *apiError {
return &apiError{
Status: 400,
Message: "bad query",
Kind: client.ErrorKindBadQuery,
}
}
// SnapNotFound is an error responder used when an operation is
// requested on a snap that doesn't exist.
func SnapNotFound(snapName string, err error) *apiError {
return &apiError{
Status: 404,
Message: err.Error(),
Kind: client.ErrorKindSnapNotFound,
Value: snapName,
}
}
// SnapNotInstalled is an error responder used when an operation is
// requested on a snap that is not in the system but expected to be.
func SnapNotInstalled(snapName string, err error) *apiError {
return &apiError{
Status: 400,
Message: err.Error(),
Kind: client.ErrorKindSnapNotInstalled,
Value: snapName,
}
}
// SnapRevisionNotAvailable is an error responder used when an
// operation is requested for which no revivision can be found
// in the given context (e.g. request an install from a stable
// channel when this channel is empty).
func SnapRevisionNotAvailable(snapName string, rnaErr *store.RevisionNotAvailableError) *apiError {
var value interface{} = snapName
kind := client.ErrorKindSnapRevisionNotAvailable
msg := rnaErr.Error()
if len(rnaErr.Releases) != 0 && rnaErr.Channel != "" {
thisArch := arch.DpkgArchitecture()
values := map[string]interface{}{
"snap-name": snapName,
"action": rnaErr.Action,
"channel": rnaErr.Channel,
"architecture": thisArch,
}
archOK := false
releases := make([]map[string]interface{}, 0, len(rnaErr.Releases))
for _, c := range rnaErr.Releases {
if c.Architecture == thisArch {
archOK = true
}
releases = append(releases, map[string]interface{}{
"architecture": c.Architecture,
"channel": c.Name,
})
}
// we return all available releases (arch x channel)
// as reported in the store error, but we hint with
// the error kind whether there was anything at all
// available for this architecture
if archOK {
kind = client.ErrorKindSnapChannelNotAvailable
msg = "no snap revision on specified channel"
} else {
kind = client.ErrorKindSnapArchitectureNotAvailable
msg = "no snap revision on specified architecture"
}
values["releases"] = releases
value = values
}
return &apiError{
Status: 404,
Message: msg,
Kind: kind,
Value: value,
}
}
// SnapChangeConflict is an error responder used when an operation would
// conflict with another ongoing change.
func SnapChangeConflict(cce *snapstate.ChangeConflictError) *apiError {
value := map[string]interface{}{}
if cce.Snap != "" {
value["snap-name"] = cce.Snap
}
if cce.ChangeKind != "" {
value["change-kind"] = cce.ChangeKind
}
return &apiError{
Status: 409,
Message: cce.Error(),
Kind: client.ErrorKindSnapChangeConflict,
Value: value,
}
}
// QuotaChangeConflict is an error responder used when an operation would
// conflict with another ongoing change.
func QuotaChangeConflict(qce *servicestate.QuotaChangeConflictError) *apiError {
value := map[string]interface{}{}
if qce.Quota != "" {
value["quota-name"] = qce.Quota
}
if qce.ChangeKind != "" {
value["change-kind"] = qce.ChangeKind
}
return &apiError{
Status: 409,
Message: qce.Error(),
Kind: client.ErrorKindQuotaChangeConflict,
Value: value,
}
}
// InsufficientSpace is an error responder used when an operation cannot
// be performed due to low disk space.
func InsufficientSpace(dserr *snapstate.InsufficientSpaceError) *apiError {
value := map[string]interface{}{}
if len(dserr.Snaps) > 0 {
value["snap-names"] = dserr.Snaps
}
if dserr.ChangeKind != "" {
value["change-kind"] = dserr.ChangeKind
}
return &apiError{
Status: 507,
Message: dserr.Error(),
Kind: client.ErrorKindInsufficientDiskSpace,
Value: value,
}
}
// AppNotFound is an error responder used when an operation is
// requested on a app that doesn't exist.
func AppNotFound(format string, v ...interface{}) *apiError {
return &apiError{
Status: 404,
Message: fmt.Sprintf(format, v...),
Kind: client.ErrorKindAppNotFound,
}
}
// AuthCancelled is an error responder used when a user cancelled
// the auth process.
func AuthCancelled(format string, v ...interface{}) *apiError {
return &apiError{
Status: 403,
Message: fmt.Sprintf(format, v...),
Kind: client.ErrorKindAuthCancelled,
}
}
// InterfacesUnchanged is an error responder used when an operation
// that would normally change interfaces finds it has nothing to do
func InterfacesUnchanged(format string, v ...interface{}) *apiError {
return &apiError{
Status: 400,
Message: fmt.Sprintf(format, v...),
Kind: client.ErrorKindInterfacesUnchanged,
}
}
func errToResponse(err error, snaps []string, fallback errorResponder, format string, v ...interface{}) *apiError {
var kind client.ErrorKind
var snapName string
switch err {
case store.ErrSnapNotFound:
switch len(snaps) {
case 1:
return SnapNotFound(snaps[0], err)
// store.ErrSnapNotFound should only be returned for individual
// snap queries; in all other cases something's wrong
case 0:
return InternalError("store.SnapNotFound with no snap given")
default:
return InternalError("store.SnapNotFound with %d snaps", len(snaps))
}
case store.ErrNoUpdateAvailable:
kind = client.ErrorKindSnapNoUpdateAvailable
case store.ErrLocalSnap:
kind = client.ErrorKindSnapLocal
default:
handled := true
switch err := err.(type) {
case *store.RevisionNotAvailableError:
// store.ErrRevisionNotAvailable should only be returned for
// individual snap queries; in all other cases something's wrong
switch len(snaps) {
case 1:
return SnapRevisionNotAvailable(snaps[0], err)
case 0:
return InternalError("store.RevisionNotAvailable with no snap given")
default:
return InternalError("store.RevisionNotAvailable with %d snaps", len(snaps))
}
case *snap.AlreadyInstalledError:
kind = client.ErrorKindSnapAlreadyInstalled
snapName = err.Snap
case *snap.NotInstalledError:
kind = client.ErrorKindSnapNotInstalled
snapName = err.Snap
case *servicestate.QuotaChangeConflictError:
return QuotaChangeConflict(err)
case *snapstate.SnapNeedsDevModeError:
kind = client.ErrorKindSnapNeedsDevMode
snapName = err.Snap
case *snapstate.SnapNeedsClassicError:
kind = client.ErrorKindSnapNeedsClassic
snapName = err.Snap
case *snapstate.SnapNeedsClassicSystemError:
kind = client.ErrorKindSnapNeedsClassicSystem
snapName = err.Snap
case *snapstate.SnapNotClassicError:
kind = client.ErrorKindSnapNotClassic
snapName = err.Snap
case *snapstate.InsufficientSpaceError:
return InsufficientSpace(err)
case net.Error:
if err.Timeout() {
kind = client.ErrorKindNetworkTimeout
} else {
handled = false
}
case *store.SnapActionError:
// we only handle a few specific cases
_, name, e := err.SingleOpError()
if e != nil {
// 👉😎👉
return errToResponse(e, []string{name}, fallback, format)
}
handled = false
default:
// support wrapped errors
switch {
case errors.Is(err, &snapstate.ChangeConflictError{}):
var conflErr *snapstate.ChangeConflictError
if errors.As(err, &conflErr) {
return SnapChangeConflict(conflErr)
}
}
handled = false
}
if !handled {
v = append(v, err)
return fallback(format, v...)
}
}
return &apiError{
Status: 400,
Message: err.Error(),
Kind: kind,
Value: snapName,
}
}