Skip to content
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

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions internal/quickstart/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,35 @@ func confirmedFlag(flag flag) tea.Cmd {
}
}

type msgRequestError struct {
Copy link
Contributor Author

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.

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
Expand All @@ -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())
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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{
Expand Down
12 changes: 12 additions & 0 deletions internal/quickstart/toggle_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"

"ldcli/internal/errors"
"ldcli/internal/flags"
)

Expand Down Expand Up @@ -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())
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}

Expand Down
Loading