From 01dd57a665708534bc4db8a0af61fd2cca34424f Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Mon, 4 Apr 2022 15:05:35 +0200 Subject: [PATCH] more panic handling --- go.mod | 1 + go.sum | 2 ++ screen.go | 24 ++++++++++++++---------- tests/screen_test.go | 2 ++ 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 2e5e446..ada84ff 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.tordarus.net/Tordarus/tui go 1.18 require ( + git.tordarus.net/Tordarus/adverr v0.2.0 git.tordarus.net/Tordarus/buf2d v1.1.2 github.com/gdamore/tcell v1.4.0 github.com/mattn/go-runewidth v0.0.7 diff --git a/go.sum b/go.sum index a1cdd8d..f944f67 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.tordarus.net/Tordarus/adverr v0.2.0 h1:kLYjR2/Vb2GHiSAMvAv+WPNaHR9BRphKanf8H/pCZdA= +git.tordarus.net/Tordarus/adverr v0.2.0/go.mod h1:XRf0+7nhOkIEr0gi9DUG4RvV2KaOFB0fYPDaR1KLenw= git.tordarus.net/Tordarus/buf2d v1.1.2 h1:mmK3tARa30gCh4WaYoSEF5e7qk0C+1ODhxerUcfXN5M= git.tordarus.net/Tordarus/buf2d v1.1.2/go.mod h1:XXPpS8nQK0gUI0ki7ftV/qlprsGCRWFVSD4ybvDuUL8= github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= diff --git a/screen.go b/screen.go index 79750eb..6d9be93 100644 --- a/screen.go +++ b/screen.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" + "git.tordarus.net/Tordarus/adverr" "git.tordarus.net/Tordarus/buf2d" "github.com/gdamore/tcell" ) @@ -65,14 +66,14 @@ func (s *Screen) StopWithError(err error) { func (s *Screen) onKeyPressed(event *KeyEvent) { if s.KeyPressed == nil || !s.KeyPressed(event) { - s.Root.OnKeyPressed(event) + go s.Root.OnKeyPressed(event) } s.Redraw() } func (s *Screen) onMouseClicked(event *MouseEvent) { if s.MouseClicked == nil || !s.MouseClicked(event) { - s.Root.OnMouseClicked(event) + go s.Root.OnMouseClicked(event) } if event.Button != MouseButtonNone { s.Redraw() @@ -93,25 +94,24 @@ func (s *Screen) Redraw() { } func (s *Screen) eventloop() { - defer s.stopOnPanic() + defer s.handlePanic("panicked while handling event") for evt := s.scr.PollEvent(); evt != nil; evt = s.scr.PollEvent() { switch event := evt.(type) { case *tcell.EventResize: - go s.Redraw() + s.Redraw() case *tcell.EventKey: - go s.onKeyPressed(event) + s.onKeyPressed(event) case *tcell.EventMouse: - go s.onMouseClicked(convertMouseEvent(event)) + s.onMouseClicked(convertMouseEvent(event)) default: s.StopWithError(errors.New(fmt.Sprintf("%#v", event))) } } - s.StopWithError(errors.New("unknown error occured")) } func (s *Screen) drawloop() { - defer s.stopOnPanic() + defer s.handlePanic("panicked while redrawing") for range s.redrawCh { w, h := s.scr.Size() @@ -126,8 +126,12 @@ func (s *Screen) drawloop() { } } -func (s *Screen) stopOnPanic() { +func (s *Screen) handlePanic(msg string) { if err := recover(); err != nil { - s.StopWithError(fmt.Errorf("%v", err)) + if e, ok := err.(error); ok { + s.StopWithError(adverr.Wrap(msg, e)) + } else { + s.StopWithError(adverr.Wrap(msg, fmt.Errorf("%v", err))) + } } } diff --git a/tests/screen_test.go b/tests/screen_test.go index 45820da..2f25eb3 100644 --- a/tests/screen_test.go +++ b/tests/screen_test.go @@ -71,6 +71,8 @@ func TestScrollView(t *testing.T) { } else if event.Button == tui.MouseWheelDown { scrollView.Scroll(1, 0) return true + } else if event.Button == tui.MouseButtonMiddle { + panic("hi") } return false