Skip to content

Commit

Permalink
fix: wrap ErrProgramKilled error
Browse files Browse the repository at this point in the history
This gives the caller more context on _why_ the program was killed by
wrapping the error with the context error.
  • Loading branch information
aymanbagabas committed Aug 12, 2024
1 parent cae9acd commit d6a19f0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func (p *Program) Run() (Model, error) {
model, err := p.eventLoop(model, cmds)
killed := p.ctx.Err() != nil
if killed {
err = ErrProgramKilled
err = fmt.Errorf("%w: %s", ErrProgramKilled, p.ctx.Err())
} else {
// Ensure we rendered the final state of the model.
p.renderer.write(model.View())
Expand Down
10 changes: 7 additions & 3 deletions tea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tea
import (
"bytes"
"context"
"errors"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -46,7 +47,10 @@ func TestTeaModel(t *testing.T) {
var in bytes.Buffer
in.Write([]byte("q"))

p := NewProgram(&testModel{}, WithInput(&in), WithOutput(&buf))
ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
defer cancel()

p := NewProgram(&testModel{}, WithInput(&in), WithOutput(&buf), WithContext(ctx))
if _, err := p.Run(); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -134,7 +138,7 @@ func TestTeaKill(t *testing.T) {
}
}()

if _, err := p.Run(); err != ErrProgramKilled {
if _, err := p.Run(); !errors.Is(err, ErrProgramKilled) {
t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)
}
}
Expand All @@ -156,7 +160,7 @@ func TestTeaContext(t *testing.T) {
}
}()

if _, err := p.Run(); err != ErrProgramKilled {
if _, err := p.Run(); !errors.Is(err, ErrProgramKilled) {
t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)
}
}
Expand Down

0 comments on commit d6a19f0

Please sign in to comment.