Skip to content

Commit dc9347f

Browse files
authored
Handle errors better (#28)
1 parent 9ff4e36 commit dc9347f

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

clients/api/client.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,15 @@ func (c *Client) doRequestInternal(method, path string, body interface{}, respon
8585
// Handle error responses
8686
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
8787
var errResp ErrorResponse
88-
if err := json.Unmarshal(respBody, &errResp); err == nil {
89-
// Check for new error format first
90-
if errResp.Error != nil {
91-
return &APIError{
92-
StatusCode: errResp.Error.StatusCode,
93-
InternalCode: errResp.Error.InternalCode,
94-
Message: errResp.Error.ErrorString,
95-
ErrorType: errResp.Error.ErrorString,
96-
}
97-
}
98-
99-
// Fall back to legacy format
100-
message := errResp.ErrorString
101-
if message == "" {
102-
message = errResp.Message
103-
}
104-
if message == "" {
105-
message = string(respBody)
106-
}
88+
if err := json.Unmarshal(respBody, &errResp); err == nil && errResp.Error != nil {
10789
return &APIError{
108-
StatusCode: resp.StatusCode,
109-
Message: message,
110-
ErrorType: message,
111-
InternalCode: 0, // Legacy errors don't have internal codes
90+
StatusCode: errResp.Error.StatusCode,
91+
InternalCode: errResp.Error.InternalCode,
92+
Message: errResp.Error.ErrorString,
93+
ErrorType: errResp.Error.ErrorString,
11294
}
11395
}
96+
// Fallback for unexpected error format
11497
return &APIError{
11598
StatusCode: resp.StatusCode,
11699
Message: string(respBody),

clients/api/errors.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
const (
1414
// Authentication & Authorization Errors (2000-2099)
1515
ErrorCodeUnauthorized = 2000
16-
ErrorCodeInvalidToken = 2001
16+
ErrorCodeInvalidToken = 2001 // Used for expired tokens
1717
ErrorCodeInvalidUserCode = 2002
1818
ErrorCodeTokenNotFound = 2003
1919
ErrorCodeInvalidDeviceCode = 2004
@@ -42,15 +42,9 @@ type AppErrorDetail struct {
4242
StatusCode int `json:"status_code"`
4343
}
4444

45-
// ErrorResponse represents an error response from the API
46-
// Supports both new format (error object) and legacy format (error string)
45+
// ErrorResponse represents an error response from the API (new format only)
4746
type ErrorResponse struct {
48-
// New format
4947
Error *AppErrorDetail `json:"error,omitempty"`
50-
51-
// Legacy format (for backward compatibility)
52-
ErrorString string `json:"error_description,omitempty"`
53-
Message string `json:"message,omitempty"`
5448
}
5549

5650
// APIError represents an API error with status code and message
@@ -153,6 +147,11 @@ func IsForceUpgrade(err error) bool {
153147
return errors.As(err, &forceUpgradeErr)
154148
}
155149

150+
// IsTokenExpired checks if the error is a token expiration error
151+
func IsTokenExpired(err error) bool {
152+
return HasErrorCode(err, ErrorCodeInvalidToken)
153+
}
154+
156155
// CheckErr checks for errors and prints appropriate messages using the command's output
157156
// Returns true if no error (ok to continue), false if there was an error
158157
func CheckErr(cmd *cobra.Command, err error) bool {
@@ -181,6 +180,27 @@ func CheckErr(cmd *cobra.Command, err error) bool {
181180
return false
182181
}
183182

183+
// Check if it's a token expiration error
184+
if IsTokenExpired(err) {
185+
// Create styled error message box
186+
errorStyle := lipgloss.NewStyle().
187+
Bold(true).
188+
Foreground(lipgloss.Color("#FF5F87")).
189+
Padding(1, 2).
190+
Border(lipgloss.RoundedBorder()).
191+
BorderForeground(lipgloss.Color("#FF5F87"))
192+
193+
commandStyle := lipgloss.NewStyle().
194+
Bold(true).
195+
Foreground(lipgloss.Color("#87D7FF"))
196+
197+
message := fmt.Sprintf("Your session has expired!\n\nRun %s to login again.",
198+
commandStyle.Render("major user login"))
199+
200+
cmd.Println(errorStyle.Render(message))
201+
return false
202+
}
203+
184204
// Check if it's a no token error
185205
if IsNoToken(err) {
186206
// Create styled error message box

0 commit comments

Comments
 (0)