From 71f1e8da1ec83813dcf81835160017e3f851d2ab Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Tue, 29 Oct 2024 15:22:30 -0500 Subject: [PATCH 1/3] feat: Wrap error returned from run with context error --- tea.go | 2 +- tea_test.go | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tea.go b/tea.go index ca11a1e546..f76b4bc1f4 100644 --- a/tea.go +++ b/tea.go @@ -810,7 +810,7 @@ func (p *Program) Run() (Model, error) { model, err := p.eventLoop(model, cmds) killed := p.ctx.Err() != nil if killed { - err = fmt.Errorf("%w: %s", ErrProgramKilled, p.ctx.Err()) + err = fmt.Errorf("%w: %w", ErrProgramKilled, p.ctx.Err()) } else { // Ensure we rendered the final state of the model. p.renderer.render(model.View()) //nolint:errcheck diff --git a/tea_test.go b/tea_test.go index 8bc16ced84..95d13857e7 100644 --- a/tea_test.go +++ b/tea_test.go @@ -160,9 +160,14 @@ func TestTeaContext(t *testing.T) { } }() - if _, err := p.Run(); !errors.Is(err, ErrProgramKilled) { + _, err := p.Run() + if !errors.Is(err, ErrProgramKilled) { t.Fatalf("Expected %v, got %v", ErrProgramKilled, err) } + + if !errors.Is(err, context.Canceled) { + t.Fatalf("Expected %v, got %v", context.Canceled, err) + } } func TestTeaBatchMsg(t *testing.T) { From 3d026eccf01ec6ffc2b7b3d6d91c02fe6577ae82 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Tue, 29 Oct 2024 15:35:53 -0500 Subject: [PATCH 2/3] feat: use context.Cause to get context error This allows for better introspection into cause of cancellation --- tea.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tea.go b/tea.go index f76b4bc1f4..bbc0d1d4e9 100644 --- a/tea.go +++ b/tea.go @@ -810,7 +810,7 @@ func (p *Program) Run() (Model, error) { model, err := p.eventLoop(model, cmds) killed := p.ctx.Err() != nil if killed { - err = fmt.Errorf("%w: %w", ErrProgramKilled, p.ctx.Err()) + err = fmt.Errorf("%w: %w", ErrProgramKilled, context.Cause(p.ctx)) } else { // Ensure we rendered the final state of the model. p.renderer.render(model.View()) //nolint:errcheck From a3d8f7d741d988dce3e65183c3d0fc61cf3e1a93 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Wed, 30 Oct 2024 11:17:22 -0500 Subject: [PATCH 3/3] build: bump go version to 1.20 --- go.mod | 2 +- tutorials/go.mod | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index dbbd787e20..c3b7dd5d6c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/charmbracelet/bubbletea/v2 -go 1.18 +go 1.20 require ( github.com/charmbracelet/colorprofile v0.1.3 diff --git a/tutorials/go.mod b/tutorials/go.mod index 9a6345722a..68f3840ed8 100644 --- a/tutorials/go.mod +++ b/tutorials/go.mod @@ -1,6 +1,6 @@ module tutorial -go 1.18 +go 1.20 require github.com/charmbracelet/bubbletea/v2 v2.0.0-20240918180721-14cb6b5de1d2