-
Notifications
You must be signed in to change notification settings - Fork 0
feat: distinguish soft and hard errors in flow updates #7
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
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 |
|---|---|---|
|
|
@@ -155,13 +155,13 @@ func (m *BrowserModel) handleFlowUpdate(update FlowUpdate) tea.Cmd { | |
|
|
||
| case StepError: | ||
| m.err = fmt.Errorf("%s", update.Message) | ||
| if update.Message == "Browser authorization timed out" || | ||
| update.Message == "Could not open browser: exit status 1" { | ||
| // Fallback to device flow | ||
| if update.Fallback { | ||
| // Soft error — caller requested fall back to Device Code Flow. | ||
| return func() tea.Msg { | ||
| return successMsg{storage: nil, ok: false} | ||
| } | ||
| } | ||
| // Hard error — surface it to the user. | ||
| return func() tea.Msg { | ||
| return errorMsg{err: m.err} | ||
| } | ||
|
Comment on lines
156
to
167
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||||||||
| package tui | ||||||||||||||||
|
|
||||||||||||||||
| import ( | ||||||||||||||||
| "context" | ||||||||||||||||
| "os" | ||||||||||||||||
| "testing" | ||||||||||||||||
| ) | ||||||||||||||||
|
|
@@ -68,6 +69,41 @@ | |||||||||||||||
| }) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func TestSimpleManagerRunBrowserFlow(t *testing.T) { | ||||||||||||||||
| t.Run("Non-error updates do not trigger fallback", func(t *testing.T) { | ||||||||||||||||
| m := NewSimpleManager() | ||||||||||||||||
| perform := func(ctx context.Context, updates chan<- FlowUpdate) (*TokenStorage, bool, error) { | ||||||||||||||||
| updates <- FlowUpdate{Type: StepStart, Step: 1} | ||||||||||||||||
| updates <- FlowUpdate{Type: BrowserOpened} | ||||||||||||||||
| updates <- FlowUpdate{Type: StepStart, Step: 2} | ||||||||||||||||
| updates <- FlowUpdate{Type: CallbackReceived} | ||||||||||||||||
| return &TokenStorage{AccessToken: "test-token"}, true, nil | ||||||||||||||||
| } | ||||||||||||||||
| _, ok, err := m.RunBrowserFlow(context.Background(), perform) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| t.Fatalf("unexpected error: %v", err) | ||||||||||||||||
| } | ||||||||||||||||
| if !ok { | ||||||||||||||||
| t.Error("non-error updates must not trigger fallback: expected ok=true") | ||||||||||||||||
| } | ||||||||||||||||
| }) | ||||||||||||||||
|
|
||||||||||||||||
| t.Run("perform returning ok=false is propagated as fallback signal", func(t *testing.T) { | ||||||||||||||||
| m := NewSimpleManager() | ||||||||||||||||
| perform := func(ctx context.Context, updates chan<- FlowUpdate) (*TokenStorage, bool, error) { | ||||||||||||||||
| updates <- FlowUpdate{Type: StepError, Message: "browser failed"} | ||||||||||||||||
| return nil, false, nil | ||||||||||||||||
| } | ||||||||||||||||
| _, ok, err := m.RunBrowserFlow(context.Background(), perform) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| t.Fatalf("unexpected error: %v", err) | ||||||||||||||||
| } | ||||||||||||||||
| if ok { | ||||||||||||||||
| t.Error("expected ok=false when perform signals fallback needed") | ||||||||||||||||
| } | ||||||||||||||||
| }) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func TestFlowUpdateHelpers(t *testing.T) { | ||||||||||||||||
| update := FlowUpdate{ | ||||||||||||||||
| Type: StepStart, | ||||||||||||||||
|
|
@@ -105,6 +141,108 @@ | |||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func TestFlowUpdate_FallbackField(t *testing.T) { | ||||||||||||||||
| t.Run("Fallback defaults to false", func(t *testing.T) { | ||||||||||||||||
| update := FlowUpdate{Type: StepError, Message: "some hard error"} | ||||||||||||||||
|
Check failure on line 146 in tui/manager_test.go
|
||||||||||||||||
| if update.Fallback { | ||||||||||||||||
| t.Error("Fallback should default to false for hard errors") | ||||||||||||||||
| } | ||||||||||||||||
| }) | ||||||||||||||||
|
|
||||||||||||||||
| t.Run("Fallback can be set to true for soft errors", func(t *testing.T) { | ||||||||||||||||
| update := FlowUpdate{ | ||||||||||||||||
| Type: StepError, | ||||||||||||||||
|
Check failure on line 154 in tui/manager_test.go
|
||||||||||||||||
| Fallback: true, | ||||||||||||||||
| Message: "Browser authorization timed out", | ||||||||||||||||
|
Check failure on line 156 in tui/manager_test.go
|
||||||||||||||||
| } | ||||||||||||||||
| if !update.Fallback { | ||||||||||||||||
| t.Error("Fallback should be true for soft (recoverable) errors") | ||||||||||||||||
| } | ||||||||||||||||
| }) | ||||||||||||||||
|
|
||||||||||||||||
| t.Run("Fallback is ignored on non-error updates", func(t *testing.T) { | ||||||||||||||||
| update := FlowUpdate{Type: StepStart, Step: 1, Fallback: false} | ||||||||||||||||
|
Check failure on line 164 in tui/manager_test.go
|
||||||||||||||||
| if update.Fallback { | ||||||||||||||||
| t.Error("Fallback should not be set on non-error updates") | ||||||||||||||||
| } | ||||||||||||||||
| }) | ||||||||||||||||
|
Comment on lines
+162
to
+168
|
||||||||||||||||
| t.Run("Fallback is ignored on non-error updates", func(t *testing.T) { | |
| update := FlowUpdate{Type: StepStart, Step: 1, Fallback: false} | |
| if update.Fallback { | |
| t.Error("Fallback should not be set on non-error updates") | |
| } | |
| }) |
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.
This StepError update message includes the prefix "Authentication failed:", but the browser completion view also prefixes errors with "Authentication failed:". This can lead to duplicated text in the TUI (e.g., "Authentication failed: Authentication failed: …"). Prefer sending the raw error string here, or adjust the rendering to avoid double-prefixing.