Skip to content
Myr

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

2 files+57 added−25 removed

modified:examples/input/main.go+40 added-24 removed
1 unmodified line
23// A simple program that counts down from 5 and then exits.45import (6 "errors"7 "fmt"8 "log"9 "tea"1011 "tea/input"12)1313type model struct {14 input input.Model14type Model struct {15 Input input.Model16 Error error17}1819type tickMsg struct{}2021func main() {22 tea.UseSysLog("tea")2324 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 line
45 log.Fatal(err)46 }47}4845func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {49func 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 }5960 switch msg := msg.(type) {61 case tea.KeyMsg:62 switch msg.String() {
1 unmodified line
64 fallthrough65 case "esc":66 return m, tea.Quit67 }6869 // We handle errors just like any other message70 case tea.ErrMsg:71 m.Error = msg72 return m, nil73 }7459 m.input, cmd = input.Update(msg, m.input)75 m.Input, cmd = input.Update(msg, m.Input)76 return m, cmd77}7863func view(m tea.Model) string {64 if m, ok := m.(model); ok {65 help := "(esc to exit)"6667 return fmt.Sprintf(68 "What’s your favorite Pokémon?\n\n%s\n\n%s",69 input.View(m.input),70 help,71 )79func 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
1package tea23import (4 "errors"5 "fmt"6 "io"7 "log"8 "log/syslog"
35 unmodified lines
44 subscriptions []Sub45 rw io.ReadWriter46}4748// 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.51type ErrMsg struct {52 error53}5455func (e ErrMsg) String() string {56 return e.Error()57}5859// NewErrMsg is a convenience function for creating a generic ErrMsg60func NewErrMsg(s string) ErrMsg {61 return ErrMsg{errors.New(s)}62}6364// Quit is a command that tells the program to exit65func Quit() Msg {66 return quitMsg{}67}
11 unmodified lines
79 }80}8182// Start initializes the program66// TODO: error channel83func (p *Program) Start() error {84 var (85 model = p.model86 cmd Cmd
162 unmodified lines