-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: show a nicer error message when toggle a flag fails #179
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,35 @@ func confirmedFlag(flag flag) tea.Cmd { | |
} | ||
} | ||
|
||
type msgRequestError struct { | ||
code string | ||
message string | ||
} | ||
|
||
func newMsgRequestError(errStr string) (msgRequestError, error) { | ||
var e struct { | ||
Code string `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
err := json.Unmarshal([]byte(errStr), &e) | ||
if err != nil { | ||
return msgRequestError{}, err | ||
} | ||
|
||
return msgRequestError{ | ||
code: e.Code, | ||
message: e.Message, | ||
}, nil | ||
} | ||
|
||
func (e msgRequestError) Error() string { | ||
return e.message | ||
} | ||
|
||
func (e msgRequestError) IsConflict() bool { | ||
return e.code == "conflict" | ||
} | ||
|
||
func createFlag(client flags.Client, accessToken, baseUri, flagName, flagKey, projKey string) tea.Cmd { | ||
return func() tea.Msg { | ||
var existingFlag bool | ||
|
@@ -73,16 +102,21 @@ func createFlag(client flags.Client, accessToken, baseUri, flagName, flagKey, pr | |
projKey, | ||
) | ||
if err != nil { | ||
var e struct { | ||
Code string `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
_ = json.Unmarshal([]byte(err.Error()), &e) | ||
existingFlag = e.Code == "conflict" | ||
if !existingFlag { | ||
return errMsg{err: errors.NewError(fmt.Sprintf("Error creating flag: %s. Press \"ctrl + c\" to quit.", e.Message))} | ||
msgRequestErr, err := newMsgRequestError(err.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to handle this here since we don't consider a conflict an error, unlike the toggle flag command where a conflict is still an error. |
||
if err != nil { | ||
return errMsg{err: err} | ||
} | ||
|
||
if !msgRequestErr.IsConflict() { | ||
return errMsg{ | ||
err: errors.NewError( | ||
fmt.Sprintf( | ||
"Error creating flag: %s. Press \"ctrl + c\" to quit.", | ||
msgRequestErr.message, | ||
), | ||
), | ||
} | ||
} | ||
} | ||
|
||
return createdFlagMsg{flag: flag{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
|
||
"ldcli/internal/errors" | ||
"ldcli/internal/flags" | ||
) | ||
|
||
|
@@ -53,9 +54,20 @@ func (m toggleFlagModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | |
case key.Matches(msg, pressableKeys.Tab): | ||
m.flagWasEnabled = true | ||
m.enabled = !m.enabled | ||
m.err = nil | ||
return m, toggleFlag(m.client, m.accessToken, m.baseUri, m.flagKey, m.enabled) | ||
} | ||
case errMsg: | ||
msgRequestErr, err := newMsgRequestError(msg.err.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We handle the error here since we're mapping the API error response to a model-specific response. |
||
if err != nil { | ||
m.err = err | ||
return m, cmd | ||
} | ||
if msgRequestErr.IsConflict() { | ||
m.err = errors.NewError("Error toggling flag: you have toggled too quickly.") | ||
return m, cmd | ||
} | ||
|
||
m.err = msg.err | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creates a new type to encapsulate converting from the API response to something we handle in messages and models.