Okay, I think that's how error handling should work
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christian Rocha · committed by Christian Rocha · parent 9909356 · Browse files · 2 files·+57 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+57 added−25 removed
- other:+57 added−25 removed
Provenance
- Commit
d976902- Tree
94995bf- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
9909356
2 files changed+57 added−25 removed
modified:examples/input/main.go+40 added-24 removed
1 unmodified line2 3 // A simple program that counts down from 5 and then exits.4 5 import (6+ "errors"7 "fmt"8 "log"9 "tea"10 11 "tea/input"12 )13 13-type model struct {14- input input.Model14+type Model struct {15+ Input input.Model16+ Error error17 }18 19 type tickMsg struct{}20 21 func main() {22 tea.UseSysLog("tea")23 24 p := tea.NewProgram(23- model{24- input: input.DefaultModel(),25+ Model{26+ Input: input.DefaultModel(),27+ Error: nil,28 },29 update,30 view,31 []tea.Sub{29- // Just hand off the subscription to the input component30- func(m tea.Model) tea.Msg {31- if m, ok := m.(model); ok {32- return input.Blink(m.input)32+ // We just hand off the subscription to the input component, giving33+ // it the model it expects.34+ func(model tea.Model) tea.Msg {35+ m, ok := model.(Model)36+ if !ok {37+ return tea.NewErrMsg("could not perform assertion on model")38 }34- // TODO: return error35- return nil39+ return input.Blink(m.Input)40 },41 },42 )43 1 unmodified line45 log.Fatal(err)46 }47 }48 45-func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {49+func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {50 var cmd tea.Cmd47- m, _ := mdl.(model)51+ m, ok := model.(Model)52+ if !ok {53+ // When we encounter errors in Update we simply add the error to the54+ // model so we can handle it in the view. We could also return a command55+ // that does something else with the error, like logs it via IO.56+ m.Error = errors.New("could not perform assertion on model")57+ return m, nil58+ }59 60 switch msg := msg.(type) {61 case tea.KeyMsg:62 switch msg.String() {1 unmodified line64 fallthrough65 case "esc":66 return m, tea.Quit67 }68+69+ // We handle errors just like any other message70+ case tea.ErrMsg:71+ m.Error = msg72+ return m, nil73 }74 59- m.input, cmd = input.Update(msg, m.input)75+ m.Input, cmd = input.Update(msg, m.Input)76 return m, cmd77 }78 63-func view(m tea.Model) string {64- if m, ok := m.(model); ok {65- help := "(esc to exit)"66-67- return fmt.Sprintf(68- "What’s your favorite Pokémon?\n\n%s\n\n%s",69- input.View(m.input),70- help,71- )79+func view(model tea.Model) string {80+ m, ok := model.(Model)81+ if !ok {82+ return "Oh no: could not perform assertion on model."83+ } else if m.Error != nil {84+ return fmt.Sprintf("Uh oh: %s", m.Error)85 }73- // TODO: return error74- return ""86+ return fmt.Sprintf(87+ "What’s your favorite Pokémon?\n\n%s\n\n%s",88+ input.View(m.Input),89+ "(esc to quit)",90+ )91 }modified:tea.go+17 added-1 removed
1 package tea2 3 import (4+ "errors"5 "fmt"6 "io"7 "log"8 "log/syslog"35 unmodified lines44 subscriptions []Sub45 rw io.ReadWriter46 }47 48+// ErrMsg is just a regular message containing an error. We handle it in Update49+// just like a regular message by case switching. Of course, the developer50+// could also define her own errors as well.51+type ErrMsg struct {52+ error53+}54+55+func (e ErrMsg) String() string {56+ return e.Error()57+}58+59+// NewErrMsg is a convenience function for creating a generic ErrMsg60+func NewErrMsg(s string) ErrMsg {61+ return ErrMsg{errors.New(s)}62+}63+64 // Quit is a command that tells the program to exit65 func Quit() Msg {66 return quitMsg{}67 }11 unmodified lines79 }80 }81 82 // Start initializes the program66-// TODO: error channel83 func (p *Program) Start() error {84 var (85 model = p.model86 cmd Cmd162 unmodified lines