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
Provenance
- Commit
f93b752- Tree
4361d0f- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
aa6d766
3 files changed+30 added−54 removed
modified:examples/http/main.go+15 added-12 removed
13 unmodified lines14 15 const url = "https://charm.sh/"16 17 type Model struct {18- Status int19- Error error18+ status int19+ err error20 }21 22 type statusMsg int23+type errMsg error24 25 func main() {26 p := tea.NewProgram(initialize, update, view, nil)27 if err := p.Start(); err != nil {5 unmodified lines33 return Model{0, nil}, checkServer34 }35 36 func 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+ }41 42 switch msg := msg.(type) {43 44 case tea.KeyMsg:8 unmodified lines53 return m, nil54 }55 56 case statusMsg:53- m.Status = int(msg)57+ m.status = int(msg)58 return m, tea.Quit59 56- 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, nil63 64 default:65 return m, nil2 unmodified lines68 69 func 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 lines82 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 lines11 "github.com/charmbracelet/teaparty/input"12 )13 14 type Model struct {15- Input input.Model16- Error error15+ textInput input.Model16+ err error17 }18 19 type tickMsg struct{}20+type errMsg error21 22 func main() {22- tea.UseSysLog("tea")23-23 p := tea.NewProgram(24 initialize,25 update,26 view,9 unmodified lines36 inputModel := input.DefaultModel()37 inputModel.Placeholder = "Pikachu"38 39 return Model{41- Input: inputModel,42- Error: nil,40+ textInput: inputModel,41+ err: nil,42 }, nil43 }44 45 func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {2 unmodified lines48 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 }56 57 switch msg := msg.(type) {58 case tea.KeyMsg:4 unmodified lines63 return m, tea.Quit64 }65 66 // We handle errors just like any other message67- case tea.ErrMsg:68- m.Error = msg67+ case errMsg:68+ m.err = msg69 return m, nil70 }71 72- m.Input, cmd = input.Update(msg, m.Input)72+ m.textInput, cmd = input.Update(msg, m.textInput)73 return m, cmd74 }75 76 func subscriptions(model tea.Model) tea.Subs {1 unmodified line78 // 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 }86 87 func 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
1 package tea2 3 import (4- "errors"4 "io"5 "os"6 "strings"7 84 unmodified lines92 view View93 subscriptions Subscriptions94 }95 97-// 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.100-type ErrMsg struct {101- error102-}103-104-// String implements String() on the error interface for ErrMsg105-func (e ErrMsg) String() string {106- return e.Error()107-}108-109-// NewErrMsg is a convenience function for creating a generic ErrMsg110-func NewErrMsg(s string) ErrMsg {111- return ErrMsg{errors.New(s)}112-}113-114-// NewErrMsgFromErr is a convenience function for creating an ErrMsg from an115-// existing error116-func NewErrMsgFromErr(e error) ErrMsg {117- return ErrMsg{e}118-}119-120-// ModelAssertionErr can be used when a model assertion didn't go as planned121-var ModelAssertionErr = NewErrMsg("could not perform assertion on model")122-96 // Quit is a command that tells the program to exit97 func Quit() Msg {98 return quitMsg{}99 }207 unmodified lines