commit 6ec6d3cadb16e1e395fd7a61fc62b8502b0e505d
parent 30f96bd54dad0395446301a0197cf0112d316cb6
Author: FIGBERT <figbert@figbert.com>
Date: Wed, 20 Mar 2024 12:03:10 -0700
Display errors to user
Diffstat:
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/api/api.go b/api/api.go
@@ -10,13 +10,13 @@ import (
type PageMsg []Post
type PostMsg Post
-type ErrorMsg struct{}
+type ErrorMsg error
func FetchPage(index int) tea.Cmd {
return func() tea.Msg {
posts, err := page(index)
if err != nil {
- return ErrorMsg{}
+ return ErrorMsg(err)
}
return PageMsg(posts)
@@ -27,14 +27,14 @@ func FetchPost(id string) tea.Cmd {
return func() tea.Msg {
resp, err := http.Get(fmt.Sprintf("https://lobste.rs/s/%s.json", id))
if err != nil {
- return ErrorMsg{}
+ return ErrorMsg(err)
}
defer resp.Body.Close()
var post Post
err = json.NewDecoder(resp.Body).Decode(&post)
if err != nil {
- return ErrorMsg{}
+ return ErrorMsg(err)
}
return PostMsg(post)
}
diff --git a/main.go b/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "fmt"
"strings"
"git.figbert.com/caret/api"
@@ -63,7 +64,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case api.PostMsg:
cmds = append(cmds, comments.Load(api.Post(msg)))
case api.ErrorMsg:
- m.msg = "Failed to fetch new stories."
+ m.msg = fmt.Sprintf("Failed to fetch new stories -> %s", msg)
cmds = append(cmds, clearMsg)
case loading:
m.msg = "Loading…"