From 15c9b9308d353d558f7c4fc2fe03012afc84be40 Mon Sep 17 00:00:00 2001 From: Aria Date: Fri, 3 Feb 2023 15:48:00 +0000 Subject: better error handling --- confirm.go | 3 +++ editing.go | 8 ++++++-- main.go | 2 ++ selecting.go | 5 ++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/confirm.go b/confirm.go index a7d4748..31ce746 100644 --- a/confirm.go +++ b/confirm.go @@ -82,6 +82,9 @@ func (m confirmModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.viewport.Width = msg.Width - padding_h m.viewport.Height = msg.Height - verticalMarginHeight - padding_v } + case fatalErrorMsg: + fmt.Printf("encountered fatal error: %s\n", msg.Error()) + return m, tea.Quit } // Handle keyboard and mouse events in the viewport diff --git a/editing.go b/editing.go index f5369d3..3ce6fd0 100644 --- a/editing.go +++ b/editing.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "os/exec" @@ -43,6 +44,9 @@ func (m detailsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // exit without saving return m.returnTo, m.returnTo.Init() } + case fatalErrorMsg: + fmt.Printf("encountered fatal error: %s\n", msg.Error()) + return m, tea.Quit } m.ti, cmd = m.ti.Update(msg) return m, cmd @@ -70,7 +74,7 @@ func saveSnippetCmd(snippet snippetDetails) tea.Cmd { return func() tea.Msg { err := saveSnippet(snippet) if err != nil { - panic(err) + return fatalErrorMsg(err) } return updateListMsg{} @@ -97,7 +101,7 @@ func deleteItemCmd(item item) tea.Cmd { return func() tea.Msg { err := deleteItem(item) if err != nil { - panic(err) + return fatalErrorMsg(err) } return updateListMsg{} diff --git a/main.go b/main.go index c4cb4f2..4bee772 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,8 @@ type item struct { contents string } +type fatalErrorMsg error + func (i item) Title() string { return i.title } func (i item) Description() string { return i.contents } func (i item) FilterValue() string { return i.title } diff --git a/selecting.go b/selecting.go index 7eeae72..0817d3d 100644 --- a/selecting.go +++ b/selecting.go @@ -78,12 +78,15 @@ func (m selectingModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, func() tea.Msg { items, err := getItems() if err != nil { - panic(err) + return fatalErrorMsg(err) } return setListItemsMsg{items} }) + case fatalErrorMsg: + fmt.Printf("encountered fatal error: %s\n", msg.Error()) + return m, tea.Quit } // This will also call our delegate's update function. -- cgit v1.2.3