Skip to content
Myr

Remove ErrMsg and ModelAssertionErr

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 aa6d766 · Browse files · 3 files+30 added−54 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+30 added−54 removed

  • other:+30 added−54 removed

3 files+30 added−54 removed

modified:examples/http/main.go+15 added-12 removed
13 unmodified lines
1415const url = "https://charm.sh/"1617type Model struct {18 Status int19 Error error18 status int19 err error20}2122type statusMsg int23type errMsg error2425func main() {26 p := tea.NewProgram(initialize, update, view, nil)27 if err := p.Start(); err != nil {
5 unmodified lines
33 return Model{0, nil}, checkServer34}3536func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {36 m, _ := model.(Model)37 m, ok := model.(Model)38 if !ok {39 return Model{err: errors.New("could not perform assertion on model during update")}, nil40 }4142 switch msg := msg.(type) {4344 case tea.KeyMsg:
8 unmodified lines
53 return m, nil54 }5556 case statusMsg:53 m.Status = int(msg)57 m.status = int(msg)58 return m, tea.Quit5956 case tea.ErrMsg:57 // TODO: get the error out of tea.ErrMsg less hackily58 m.Error = errors.New(msg.Error())60 case errMsg:61 m.err = msg62 return m, nil6364 default:65 return m, nil
2 unmodified lines
6869func view(model tea.Model) string {70 m, _ := model.(Model)71 s := fmt.Sprintf("Checking %s...", url)69 if m.Error != nil {70 s += fmt.Sprintf("something went wrong: %s", m.Error)71 } else if m.Status != 0 {72 s += fmt.Sprintf("%d %s", m.Status, http.StatusText(m.Status))72 if m.err != nil {73 s += fmt.Sprintf("something went wrong: %s", m.err)74 } else if m.status != 0 {75 s += fmt.Sprintf("%d %s", m.status, http.StatusText(m.status))76 }77 return s78}79
2 unmodified lines
82 Timeout: 10 * time.Second,83 }84 res, err := c.Get(url)85 if err != nil {83 return tea.NewErrMsg(err.Error())86 return errMsg(err)87 }88 return statusMsg(res.StatusCode)89}
modified:examples/input/main.go+15 added-15 removed
10 unmodified lines
11 "github.com/charmbracelet/teaparty/input"12)1314type Model struct {15 Input input.Model16 Error error15 textInput input.Model16 err error17}1819type tickMsg struct{}20type errMsg error2122func main() {22 tea.UseSysLog("tea")2323 p := tea.NewProgram(24 initialize,25 update,26 view,
9 unmodified lines
36 inputModel := input.DefaultModel()37 inputModel.Placeholder = "Pikachu"3839 return Model{41 Input: inputModel,42 Error: nil,40 textInput: inputModel,41 err: nil,42 }, nil43}4445func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
2 unmodified lines
48 if !ok {49 // When we encounter errors in Update we simply add the error to the50 // model so we can handle it in the view. We could also return a command51 // that does something else with the error, like logs it via IO.53 m.Error = errors.New("could not perform assertion on model")54 return m, nil52 return Model{53 err: errors.New("could not perform assertion on model in update"),54 }, nil55 }5657 switch msg := msg.(type) {58 case tea.KeyMsg:
4 unmodified lines
63 return m, tea.Quit64 }6566 // We handle errors just like any other message67 case tea.ErrMsg:68 m.Error = msg67 case errMsg:68 m.err = msg69 return m, nil70 }7172 m.Input, cmd = input.Update(msg, m.Input)72 m.textInput, cmd = input.Update(msg, m.textInput)73 return m, cmd74}7576func subscriptions(model tea.Model) tea.Subs {
1 unmodified line
78 // We just hand off the subscription to the input component, giving79 // it the model it expects.80 "input": func(model tea.Model) tea.Msg {81 m, _ := model.(Model)82 return input.Blink(m.Input)82 return input.Blink(m.textInput)83 },84 }85}8687func view(model tea.Model) string {88 m, ok := model.(Model)89 if !ok {90 return "Oh no: could not perform assertion on model."91 } else if m.Error != nil {92 return fmt.Sprintf("Uh oh: %s", m.Error)91 } else if m.err != nil {92 return fmt.Sprintf("Uh oh: %s", m.err)93 }94 return fmt.Sprintf(95 "What’s your favorite Pokémon?\n\n%s\n\n%s",96 input.View(m.Input),96 input.View(m.textInput),97 "(esc to quit)",98 )99}
modified:tea.go-27 removed
1package tea23import (4 "errors"4 "io"5 "os"6 "strings"7
84 unmodified lines
92 view View93 subscriptions Subscriptions94}9597// ErrMsg is just a regular message containing an error. We handle it in Update98// just like a regular message by case switching. Of course, the developer99// could also define her own errors as well.100type ErrMsg struct {101 error102}103104// String implements String() on the error interface for ErrMsg105func (e ErrMsg) String() string {106 return e.Error()107}108109// NewErrMsg is a convenience function for creating a generic ErrMsg110func NewErrMsg(s string) ErrMsg {111 return ErrMsg{errors.New(s)}112}113114// NewErrMsgFromErr is a convenience function for creating an ErrMsg from an115// existing error116func NewErrMsgFromErr(e error) ErrMsg {117 return ErrMsg{e}118}119120// ModelAssertionErr can be used when a model assertion didn't go as planned121var ModelAssertionErr = NewErrMsg("could not perform assertion on model")12296// Quit is a command that tells the program to exit97func Quit() Msg {98 return quitMsg{}99}
207 unmodified lines