-
Notifications
You must be signed in to change notification settings - Fork 240
/
flyerr.go
94 lines (75 loc) · 1.6 KB
/
flyerr.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
package flyerr
import (
"context"
"errors"
"fmt"
"github.com/logrusorgru/aurora"
)
// ErrAbort is an error for when the CLI aborts
var ErrAbort = errors.New("abort")
// ErrorDescription is an error with a detailed description that will be printed before the CLI exits
type ErrorDescription interface {
error
Description() string
}
func GetErrorDescription(err error) string {
var ferr ErrorDescription
if errors.As(err, &ferr) {
return ferr.Description()
}
return ""
}
// ErrorSuggestion is an error with suggested next steps that will be printed before the CLI exits
type ErrorSuggestion interface {
error
Suggestion() string
}
func GetErrorSuggestion(err error) string {
var ferr ErrorSuggestion
if errors.As(err, &ferr) {
return ferr.Suggestion()
}
return ""
}
func PrintCLIOutput(err error) {
if err == nil {
return
}
if IsCancelledError(err) {
return
}
fmt.Println()
fmt.Println(aurora.Red("Error"), err)
description := GetErrorDescription(err)
suggestion := GetErrorSuggestion(err)
if description != "" {
fmt.Printf("\n%s", description)
}
if suggestion != "" {
if description != "" {
fmt.Println()
}
fmt.Printf("\n%s", suggestion)
}
fmt.Println()
}
func IsCancelledError(err error) bool {
if errors.Is(err, ErrAbort) {
return true
}
if errors.Is(err, context.Canceled) {
return true
}
// if err == cmd.ErrAbort {
// return true
// }
// if err == context.Canceled {
// return true
// }
// if merr, ok := err.(*multierror.Error); ok {
// if len(merr.Errors) == 1 && merr.Errors[0] == context.Canceled {
// return true
// }
// }
return false
}