Replies: 4 comments 3 replies
-
The |
Beta Was this translation helpful? Give feedback.
-
Hi I have the same question, it seems like case err := <-p.errs:
return model, err was never been used to capture error |
Beta Was this translation helpful? Give feedback.
-
@meowgorithm Can we please add a new ErrMsg to the main event loop as this would easily solve this issue of returning an error from bubbletea to the calling program and exiting? Something like: // ErrorCmd is a special command that tells the Bubble Tea program to exit.
func ErrorCmd(err error) Msg {
return ErrorMsg{err: err}
}
// ErrorMsg signals that the program should quit. You can send a ErrorMsg with
// Error.
type ErrorMsg struct {
err error
}
func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
// ...
case ErrorMsg:
return model, msg.err
// ....
} |
Beta Was this translation helpful? Give feedback.
-
Hi @kalensk (and others)! I don't follow what you're trying to do exactly. If it's to surface an error you found within your program, why not do something like the following? package main
import (
"fmt"
"net/http"
"os"
tea "github.com/charmbracelet/bubbletea"
)
func main() {
p := tea.NewProgram(Model{})
m, err := p.Run()
if err != nil {
fmt.Fprint(os.Stderr, "Error running program:", err)
}
// Check our model for any errors that may have occurred.
model := m.(Model)
if model.Error != nil {
fmt.Fprint(os.Stderr, "When we ran that program there was quite an error!", model.Error.err)
}
}
type Model struct {
Status int
Error *HTTPErrorMsg
}
func (m Model) Init() tea.Cmd {
return HTTPCmd("https://charm.sh/login")
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "esc", "ctrl+c":
return m, tea.Quit
}
case HTTPStatusMsg:
m.Status = int(msg)
return m, tea.Quit
case *HTTPErrorMsg:
m.Error = msg
return m, tea.Quit
}
return m, nil
}
func (m Model) View() string {
if m.Error != nil {
return "Ugh!\n"
}
if m.Status == 0 {
return "Hang on...\n"
}
return fmt.Sprintf("Done: %s (%d)\n", http.StatusText(m.Status), m.Status)
}
type HTTPStatusMsg int
type HTTPErrorMsg struct{ err error }
func (e HTTPErrorMsg) Error() string {
return e.err.Error()
}
func HTTPCmd(url string) tea.Cmd {
return func() tea.Msg {
res, err := http.Get(url)
if err != nil {
return &HTTPErrorMsg{err}
}
return HTTPStatusMsg(res.StatusCode)
}
} If that's not what you're looking to do, can you produce a very small example to illustrate what you'd like to solve for? |
Beta Was this translation helpful? Give feedback.
-
I would like to bubble up errors from the bubbletea program back to my calling code to exit.
Is there a command like
tea.Quit
that can return an error back totea.NewProgram
to error and bubble up to my calling code?In
tea.go
I see the following snippet which appears to not quite support what I am looking for?Beta Was this translation helpful? Give feedback.
All reactions