-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy patherrors.go
291 lines (250 loc) · 8.63 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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2018 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 store
import (
"errors"
"fmt"
"net/url"
"sort"
"strings"
"github.com/snapcore/snapd/snap/channel"
"github.com/snapcore/snapd/strutil"
)
var (
// ErrBadQuery is returned from Find when the query has special characters in strange places.
ErrBadQuery = errors.New("bad query")
// ErrInvalidScope is returned from Find when an invalid scope is requested.
ErrInvalidScope = errors.New("invalid scope")
// ErrSnapNotFound is returned when a snap can not be found
ErrSnapNotFound = errors.New("snap not found")
// ErrUnauthenticated is returned when authentication is needed to complete the query
ErrUnauthenticated = errors.New("you need to log in first")
// ErrAuthenticationNeeds2fa is returned if the authentication needs 2factor
ErrAuthenticationNeeds2fa = errors.New("two factor authentication required")
// Err2faFailed is returned when 2fa failed (e.g., a bad token was given)
Err2faFailed = errors.New("two factor authentication failed")
// ErrInvalidCredentials is returned on login error
// It can also be returned when refreshing the discharge
// macaroon if the user has changed their password.
ErrInvalidCredentials = errors.New("invalid credentials")
// ErrTOSNotAccepted is returned when the user has not accepted the store's terms of service.
ErrTOSNotAccepted = errors.New("terms of service not accepted")
// ErrNoPaymentMethods is returned when the user has no valid payment methods associated with their account.
ErrNoPaymentMethods = errors.New("no payment methods")
// ErrPaymentDeclined is returned when the user's payment method was declined by the upstream payment provider.
ErrPaymentDeclined = errors.New("payment declined")
// ErrLocalSnap is returned when an operation that only applies to snaps that come from a store was attempted on a local snap.
ErrLocalSnap = errors.New("cannot perform operation on local snap")
// ErrNoUpdateAvailable is returned when an update is attempetd for a snap that has no update available.
ErrNoUpdateAvailable = errors.New("snap has no updates available")
)
// RevisionNotAvailableError is returned when an install is attempted for a snap but the/a revision is not available (given install constraints).
type RevisionNotAvailableError struct {
Action string
Channel string
Releases []channel.Channel
}
func (e *RevisionNotAvailableError) Error() string {
return "no snap revision available as specified"
}
// DownloadError represents a download error
type DownloadError struct {
Code int
URL *url.URL
}
func (e *DownloadError) Error() string {
return fmt.Sprintf("received an unexpected http response code (%v) when trying to download %s", e.Code, e.URL)
}
// PasswordPolicyError is returned in a few corner cases, most notably
// when the password has been force-reset.
type PasswordPolicyError map[string]stringList
func (e PasswordPolicyError) Error() string {
var msg string
if reason, ok := e["reason"]; ok && len(reason) == 1 {
msg = reason[0]
if location, ok := e["location"]; ok && len(location) == 1 {
msg += "\nTo address this, go to: " + location[0] + "\n"
}
} else {
for k, vs := range e {
msg += fmt.Sprintf("%s: %s\n", k, strings.Join(vs, " "))
}
}
return msg
}
// InvalidAuthDataError signals that the authentication data didn't pass validation.
type InvalidAuthDataError map[string]stringList
func (e InvalidAuthDataError) Error() string {
var es []string
for _, v := range e {
es = append(es, v...)
}
// XXX: confirm with server people that extra args are all
// full sentences (with periods and capitalization)
// (empirically this checks out)
return strings.Join(es, " ")
}
// SnapActionError conveys errors that were reported on otherwise overall successful snap action (install/refresh) request.
type SnapActionError struct {
// NoResults is set if there were no results in the response
NoResults bool
// Refresh errors by snap name.
Refresh map[string]error
// Install errors by snap name.
Install map[string]error
// Download errors by snap name.
Download map[string]error
// Other errors.
Other []error
}
// SingleOpError returns the single operation, snap name, and error if
// e represents a single error of a single operation on a single snap
// (i.e. if e.Other is empty, and e.Refresh, e.Install and e.Download
// have a single error in total).
// In any other case, the error returned will be nil.
func (e SnapActionError) SingleOpError() (op, name string, err error) {
if len(e.Other) > 0 {
return "", "", nil
}
nRefresh := len(e.Refresh)
nInstall := len(e.Install)
nDownload := len(e.Download)
if nRefresh+nInstall+nDownload != 1 {
return "", "", nil
}
var errs map[string]error
switch {
case nRefresh > 0:
op = "refresh"
errs = e.Refresh
case nInstall > 0:
op = "install"
errs = e.Install
case nDownload > 0:
op = "download"
errs = e.Download
}
for name, err = range errs {
return op, name, err
}
// can't happen
return "", "", nil
}
func (e SnapActionError) Error() string {
nRefresh := len(e.Refresh)
nInstall := len(e.Install)
nDownload := len(e.Download)
nOther := len(e.Other)
// single error
switch nRefresh + nInstall + nDownload + nOther {
case 0:
if e.NoResults {
// this is an atypical result
return "no install/refresh information results from the store"
}
case 1:
if nOther == 0 {
op, name, err := e.SingleOpError()
return fmt.Sprintf("cannot %s snap %q: %v", op, name, err)
} else {
return fmt.Sprintf("cannot refresh, install, or download: %v", e.Other[0])
}
}
header := "cannot refresh, install, or download:"
if nOther == 0 {
// at least one of nDownload, nInstall, or nRefresh is > 0
switch {
case nDownload == 0 && nRefresh == 0:
header = "cannot install:"
case nDownload == 0 && nInstall == 0:
header = "cannot refresh:"
case nRefresh == 0 && nInstall == 0:
header = "cannot download:"
case nDownload == 0:
header = "cannot refresh or install:"
case nInstall == 0:
header = "cannot refresh or download:"
case nRefresh == 0:
header = "cannot install or download:"
}
}
// reverse the "snap->error" map to "error->snap", as the
// common case is that all snaps fail with the same error
// (e.g. "no refresh available")
errToSnaps := map[string][]string{}
errKeys := []string{} // poorman's ordered map
for _, m := range []map[string]error{e.Refresh, e.Install, e.Download} {
for snapName, err := range m {
k := err.Error()
v, ok := errToSnaps[k]
if !ok {
errKeys = append(errKeys, k)
}
errToSnaps[k] = append(v, snapName)
}
}
es := make([]string, 1, 1+len(errToSnaps)+nOther)
es[0] = header
for _, k := range errKeys {
sort.Strings(errToSnaps[k])
es = append(es, fmt.Sprintf("%s: %s", k, strutil.Quoted(errToSnaps[k])))
}
for _, e := range e.Other {
es = append(es, e.Error())
}
if len(es) == 2 {
// header + 1 reason
return strings.Join(es, " ")
}
return strings.Join(es, "\n")
}
// Authorization soft-expiry errors that get handled automatically.
var (
errUserAuthorizationNeedsRefresh = errors.New("soft-expired user authorization needs refresh")
errDeviceAuthorizationNeedsRefresh = errors.New("soft-expired device authorization needs refresh")
)
func translateSnapActionError(action, snapChannel, code, message string, releases []snapRelease) error {
switch code {
case "revision-not-found":
e := &RevisionNotAvailableError{
Action: action,
Channel: snapChannel,
}
if len(releases) != 0 {
parsedReleases := make([]channel.Channel, len(releases))
for i := 0; i < len(releases); i++ {
var err error
parsedReleases[i], err = channel.Parse(releases[i].Channel, releases[i].Architecture)
if err != nil {
// shouldn't happen, return error without Releases
return e
}
}
e.Releases = parsedReleases
}
return e
case "id-not-found", "name-not-found":
return ErrSnapNotFound
case "user-authorization-needs-refresh":
return errUserAuthorizationNeedsRefresh
case "device-authorization-needs-refresh":
return errDeviceAuthorizationNeedsRefresh
default:
return fmt.Errorf("%v", message)
}
}