-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommon_cli_errors.go
More file actions
121 lines (105 loc) · 3.03 KB
/
common_cli_errors.go
File metadata and controls
121 lines (105 loc) · 3.03 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
package common
import (
"fmt"
"net/http"
"strings"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
"github.com/stackvista/stackstate-cli/internal/util"
)
const (
HTTPStatusUnauthorized = 401
)
func NewWriteFileError(err error, filepath string) CLIError {
return StdCLIError{
Err: fmt.Errorf("cannot write to file %s (%s)", filepath, err.Error()),
exitCode: WriteFileErrorExitCode,
}
}
func NewReadFileError(err error, filepath string) CLIError {
return StdCLIError{
Err: fmt.Errorf("cannot read file %s (%s)", filepath, err.Error()),
exitCode: ReadFileErrorExitCode,
}
}
func NewResponseError(err error, serverResponse *http.Response) CLIError {
return StdCLIError{Err: err, ServerResponse: serverResponse}
}
func NewConnectError(err error, apiURL string, serverResponse *http.Response) CLIError {
var statusCode int
// is access error?
if serverResponse != nil {
statusCode = serverResponse.StatusCode
}
if statusCode == HTTPStatusUnauthorized {
var errMessage string
if apiErr, ok := err.(*stackstate_api.GenericOpenAPIError); ok {
if detailedErr, ok := apiErr.Model().(stackstate_api.GenericErrorsResponse); ok {
// Now you can access your 401 error details
var messages []string
for _, apiErr := range detailedErr.Errors {
messages = append(messages, apiErr.Message)
}
errMessage = strings.Join(messages, ", ")
// Some responses do not yield more than the 401, others give in the body the error message as the RefreshToken failed or so
if errMessage == "" {
errMessage = "invalid api-token"
}
} else {
errMessage = err.Error()
}
} else {
errMessage = err.Error()
}
return StdCLIError{
Err: fmt.Errorf("could not connect to %s: %s\n"+
"For more information: https://l.stackstate.com/cli-invalid-api-token", apiURL, errMessage),
exitCode: ConnectErrorExitCode,
}
} else {
return StdCLIError{
Err: fmt.Errorf("could not connect to %s (%s)", apiURL, err),
exitCode: ConnectErrorExitCode,
}
}
}
func NewCLIArgParseError(err error) CLIError {
return StdCLIError{
Err: err,
ServerResponse: nil,
showUsage: true,
exitCode: CommandFailedRequirementExitCode,
}
}
func CheckFlagIsValidChoice(flagName string, flagValue string, choices []string) CLIError {
if !util.StringInSlice(flagValue, choices) {
return NewCLIArgParseError(
fmt.Errorf("invalid '%s' flag value '%s' (must be { %s })", flagName, flagValue, strings.Join(choices, " | ")),
)
} else {
return nil
}
}
func NewNotFoundError(err error) CLIError {
return StdCLIError{
Err: err,
ServerResponse: nil,
showUsage: true,
exitCode: NotFoundExitCode,
}
}
func NewExecutionError(err error) CLIError {
return StdCLIError{
Err: err,
ServerResponse: nil,
showUsage: true,
exitCode: ExecutionErrorCode,
}
}
func NewAPIVersionError(err error) CLIError {
return StdCLIError{
Err: err,
ServerResponse: nil,
showUsage: true,
exitCode: APIVersionErrorCode,
}
}