-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy patherrors.go
More file actions
378 lines (318 loc) · 8.21 KB
/
Copy patherrors.go
File metadata and controls
378 lines (318 loc) · 8.21 KB
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
package errors
import (
"fmt"
"net"
"net/url"
"runtime"
"strconv"
"time"
stderrors "errors"
"github.com/getsentry/sentry-go"
pkgerrors "github.com/pkg/errors"
)
type BrevError interface {
// Error returns a user-facing string explaining the error
Error() string
// Directive returns a user-facing string explaining how to overcome the error
Directive() string
}
type ErrorUser struct {
ID string
Username string
Email string
}
type ErrorReporter interface {
Setup() func()
Flush()
ReportMessage(string) string
ReportError(error) string
AddTag(key string, value string)
SetUser(user ErrorUser)
AddBreadCrumb(bc ErrReportBreadCrumb)
}
func GetDefaultErrorReporter() ErrorReporter {
return SentryErrorReporter{}
}
type SentryErrorReporter struct{}
var _ ErrorReporter = SentryErrorReporter{}
func (s SentryErrorReporter) Setup() func() {
return func() {
err := recover()
if err != nil {
sentry.CurrentHub().Recover(err)
sentry.Flush(time.Second * 5)
panic(err)
}
sentry.Flush(2 * time.Second)
}
}
func (s SentryErrorReporter) SetUser(user ErrorUser) {
scope := sentry.CurrentHub().Scope()
scope.SetUser(sentry.User{
ID: user.ID,
Username: user.Username,
Email: user.Email,
})
}
func (s SentryErrorReporter) Flush() {
sentry.Flush(time.Second * 2)
}
type ErrReportBreadCrumb struct {
Type string
Category string
Message string
Level string
}
func (s SentryErrorReporter) AddBreadCrumb(bc ErrReportBreadCrumb) {
scope := sentry.CurrentHub()
scope.AddBreadcrumb(&sentry.Breadcrumb{
Type: bc.Type,
Category: bc.Category,
Message: bc.Message,
Level: sentry.Level(bc.Level),
}, &sentry.BreadcrumbHint{})
}
func (s SentryErrorReporter) ReportMessage(msg string) string {
event := sentry.CaptureMessage(msg)
if event != nil {
return string(*event)
} else {
return ""
}
}
func (s SentryErrorReporter) ReportError(e error) string {
event := sentry.CaptureException(e)
if event != nil {
return string(*event)
} else {
return ""
}
}
func (s SentryErrorReporter) AddTag(key string, value string) {
scope := sentry.CurrentHub().Scope()
scope.SetTag(key, value)
}
type ValidationError struct {
Message string
}
func NewValidationError(message string) ValidationError {
return ValidationError{Message: message}
}
var _ error = ValidationError{}
func (v ValidationError) Error() string {
return v.Message
}
type DeclineToLoginError struct{}
func (d *DeclineToLoginError) Error() string { return "declined to login" }
func (d *DeclineToLoginError) Directive() string { return "log in to run this command" }
var NetworkErrorMessage = "possible internet connection problem"
// NetworkError is a user-facing error for transport-level failures (DNS
// lookup failures, dial timeouts, connection refused, etc.) when reaching a
// remote service. It hides the underlying stacktrace and produces a short,
// actionable message suitable for end users.
type NetworkError struct {
// Host is the host the CLI was trying to reach (e.g. "api.ngc.nvidia.com").
// Empty if no host could be derived from the original error.
Host string
// Cause is the underlying transport error.
Cause error
}
func (e *NetworkError) Error() string {
if e.Host != "" {
return fmt.Sprintf("Could not reach %s — check your internet connection and try again", e.Host)
}
return "Could not reach the network — check your internet connection and try again"
}
func (e *NetworkError) Directive() string {
if e.Host != "" {
return fmt.Sprintf("Verify you can resolve %s and that no firewall or proxy is blocking it. If the host is reachable, the service may be temporarily unavailable.", e.Host)
}
return "Verify your internet connection. If the network is healthy, the service may be temporarily unavailable."
}
func (e *NetworkError) Unwrap() error { return e.Cause }
// IsNetworkError reports whether err (or any error in its chain) is a
// transport-level network failure such as a DNS lookup failure, dial
// timeout, or connection refusal.
func IsNetworkError(err error) bool {
if err == nil {
return false
}
var dnsErr *net.DNSError
if stderrors.As(err, &dnsErr) {
return true
}
var opErr *net.OpError
if stderrors.As(err, &opErr) {
return true
}
var netErr net.Error
if stderrors.As(err, &netErr) {
return true
}
return false
}
// HostFromURLError returns the host from a *url.Error in err's chain, or ""
// if no URL is available. Useful when wrapping HTTP client errors.
func HostFromURLError(err error) string {
var urlErr *url.Error
if !stderrors.As(err, &urlErr) {
return ""
}
parsed, perr := url.Parse(urlErr.URL)
if perr != nil || parsed == nil {
return ""
}
return parsed.Host
}
// WrapNetworkError returns a *NetworkError wrapping err if err is a
// transport-level network failure; otherwise it returns err unchanged. The
// fallbackHost is used only if the host cannot be derived from err.
func WrapNetworkError(err error, fallbackHost string) error {
if err == nil {
return nil
}
if !IsNetworkError(err) {
return err
}
host := HostFromURLError(err)
if host == "" {
host = fallbackHost
}
return &NetworkError{Host: host, Cause: err}
}
type CredentialsFileNotFound struct{}
func (e *CredentialsFileNotFound) Directive() string {
return "run `brev login`"
}
func (e *CredentialsFileNotFound) Error() string {
return "credentials file not found"
}
type WorkspaceNotRunning struct {
Status string
}
func (e WorkspaceNotRunning) Error() string {
return fmt.Sprintf("workspace status %s is not RUNNING", e.Status)
}
var New = stderrors.New
var Errorf = fmt.Errorf
func Wrap(err error, msg string) error {
if err == nil {
return nil
}
return Errorf("%s: %w", msg, err)
}
var As = stderrors.As
var Unwrap = stderrors.Unwrap
func Unwraps(err error) []error {
u, ok := err.(interface {
Unwrap() []error
})
if !ok {
return nil
}
return u.Unwrap()
}
func Root(err error) error {
for Unwrap(err) != nil {
err = Unwrap(err)
}
joinedErrs := Unwraps(err)
if len(joinedErrs) == 0 {
return err
}
return Roots(joinedErrs)
}
func Roots(errs []error) error {
if len(errs) == 0 {
return nil
}
rootedErrs := make([]error, len(errs))
for i, e := range errs {
rootedErrs[i] = Root(e)
}
return Join(rootedErrs...)
}
// flattens error tree
func Flatten(err error) []error {
if err == nil {
return nil
}
joinedErrs := Unwraps(err)
if joinedErrs == nil {
return []error{err}
}
flatErrs := []error{}
for _, e := range joinedErrs {
flatErrs = append(flatErrs, Flatten(e)...)
}
return flatErrs
}
// var ReturnTrace = errtrace.Wrap
func Join(errs ...error) error {
noNilErrs := make([]error, 0, len(errs))
for _, err := range errs {
if err != nil {
noNilErrs = append(noNilErrs, err)
}
}
if len(noNilErrs) == 0 {
return nil
}
if len(noNilErrs) == 1 {
return noNilErrs[0]
}
return stderrors.Join(errs...) //nolint:wrapcheck // this is a wrapper
}
// if multi err, combine similar errors
func CombineByString(err error) error {
if err == nil {
return nil
}
errs := Flatten(err)
mapE := make(map[string]error)
mapEList := []error{}
for _, e := range errs {
_, ok := mapE[e.Error()]
if !ok {
mapE[e.Error()] = e
mapEList = append(mapEList, e)
}
}
errsOut := make([]error, 0, len(mapE))
for _, e := range mapEList {
errsOut = append(errsOut, e)
}
return Join(errsOut...)
}
var Is = stderrors.Is
var WrapAndTrace = WrapAndTraceInMsg
func WrapAndTraceInMsg(err error) error {
if err == nil {
return nil
}
return pkgerrors.Wrap(err, makeErrorMessage("", 0)) // this wrap also adds a stacktrace which can be nice
}
func makeErrorMessage(message string, skip int) string {
skip += 2
pc, file, line, _ := runtime.Caller(skip)
funcName := "unknown"
fn := runtime.FuncForPC(pc)
if fn != nil {
funcName = fn.Name()
}
lineNum := strconv.Itoa(line)
return fmt.Sprintf("[error] %s\n%s\n%s:%s\n", message, funcName, file, lineNum)
}
// logger.L().Error("", zap.Error(err))
type NvidiaMigrationError struct {
Message string
}
func (e NvidiaMigrationError) Error() string {
return e.Message
}
func (e NvidiaMigrationError) Directive() string {
return "Please run 'brev login --auth nvidia' to log in with your NVIDIA account"
}
func NewNvidiaMigrationError(msg string) *NvidiaMigrationError {
return &NvidiaMigrationError{Message: msg}
}