-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patherrors.go
More file actions
67 lines (60 loc) · 1.89 KB
/
errors.go
File metadata and controls
67 lines (60 loc) · 1.89 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
package cmd
import "errors"
// Exit codes as defined in the CLI specification
const (
ExitSuccess = 0 // Success
ExitGeneralError = 1 // General error
ExitTimeout = 2 // Operation timeout (wait-timeout exceeded) or connection timeout
ExitInvalidParameters = 3 // Invalid parameters
ExitAuthenticationError = 4 // Authentication error
ExitPermissionDenied = 5 // Permission denied
ExitServiceNotFound = 6 // Service not found
)
// exitCodeError creates an error that will cause the program to exit with the specified code
type exitCodeError struct {
code int
err error
}
func (e exitCodeError) Error() string {
if e.err == nil {
return ""
}
return e.err.Error()
}
func (e exitCodeError) ExitCode() int {
return e.code
}
// exitWithCode returns an error that will cause the program to exit with the specified code
func exitWithCode(code int, err error) error {
return exitCodeError{code: code, err: err}
}
// exitWithErrorFromStatusCode maps HTTP status codes to CLI exit codes
func exitWithErrorFromStatusCode(statusCode int, err error) error {
if err == nil {
err = errors.New("unknown error")
}
switch statusCode {
case 400:
// Bad request - invalid parameters
return exitWithCode(ExitInvalidParameters, err)
case 401:
// Unauthorized - authentication error
return exitWithCode(ExitAuthenticationError, err)
case 403:
// Forbidden - permission denied
return exitWithCode(ExitPermissionDenied, err)
case 404:
// Not found - service/resource not found
return exitWithCode(ExitServiceNotFound, err)
case 408, 504:
// Request timeout or gateway timeout
return exitWithCode(ExitTimeout, err)
default:
// For other 4xx errors, use general error
if statusCode >= 400 && statusCode < 500 {
return exitWithCode(ExitGeneralError, err)
}
// For 5xx and other errors, use general error
return exitWithCode(ExitGeneralError, err)
}
}