-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherrors.go
More file actions
84 lines (74 loc) · 1.89 KB
/
Copy patherrors.go
File metadata and controls
84 lines (74 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cmd
import (
"errors"
"fmt"
"github.com/Use-Tusk/tusk-cli/internal/api"
)
// ExitCodeError wraps an error with a specific process exit code. main.go
// unwraps this to pick the right os.Exit value; without it, Cobra-returned
// errors map to exit 1.
type ExitCodeError struct {
Code int
Err error
}
func (e *ExitCodeError) Error() string {
if e == nil || e.Err == nil {
return ""
}
return e.Err.Error()
}
func (e *ExitCodeError) Unwrap() error {
if e == nil {
return nil
}
return e.Err
}
// ExitCodeOf returns the exit code embedded in err (or any wrapper in its
// chain), defaulting to 1 if none is present.
func ExitCodeOf(err error) int {
if err == nil {
return 0
}
var ec *ExitCodeError
if errors.As(err, &ec) {
return ec.Code
}
return 1
}
// formatApiError converts raw API errors into user-friendly messages with
// actionable guidance. Non-API errors pass through unchanged.
func formatApiError(err error) error {
if err == nil {
return nil
}
var apiErr *api.ApiError
if !errors.As(err, &apiErr) {
return err
}
switch {
case apiErr.StatusCode == 401 || apiErr.StatusCode == 403:
return fmt.Errorf("Not authorized. Your credentials may be expired or invalid.\nRun `tusk auth login` or set TUSK_API_KEY.\nGet started: %s", api.DocsSetupURL)
case apiErr.StatusCode == 404:
msg := "Resource not found"
if apiErr.Message != "" {
msg = capitalizeFirst(apiErr.Message)
}
return fmt.Errorf("%s.", msg)
case apiErr.StatusCode >= 500:
return fmt.Errorf("Tusk service error (HTTP %d). Please try again.\nIf the issue persists, please contact support@usetusk.ai.", apiErr.StatusCode)
default:
if apiErr.Message != "" {
return fmt.Errorf("%s (HTTP %d)", apiErr.Message, apiErr.StatusCode)
}
return err
}
}
func capitalizeFirst(s string) string {
if len(s) == 0 {
return s
}
if s[0] >= 'a' && s[0] <= 'z' {
return string(s[0]-32) + s[1:]
}
return s
}