diff options
author | Aria <me@aria.rip> | 2023-02-03 15:48:00 +0000 |
---|---|---|
committer | Aria <me@aria.rip> | 2023-02-03 15:48:00 +0000 |
commit | 15c9b9308d353d558f7c4fc2fe03012afc84be40 (patch) | |
tree | faff8319ffc87d10447274a12751ddc43216649d | |
parent | 8c3b1aee5a253416523de78877ae03c3868cc62c (diff) |
better error handling
-rw-r--r-- | confirm.go | 3 | ||||
-rw-r--r-- | editing.go | 8 | ||||
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | selecting.go | 5 |
4 files changed, 15 insertions, 3 deletions
@@ -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 @@ -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{} @@ -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. |