Skip to content
Myr

Error msg should be a struct in command tutorial, not a generic error

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 a2a85d3 · Browse files · 2 files+14 added−4 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+14 added−4 removed

  • other:+14 added−4 removed

2 files+14 added−4 removed

modified:tutorials/commands/main.go+7 added-2 removed
18 unmodified lines
19func checkServer() tea.Msg {20 c := &http.Client{Timeout: 10 * time.Second}21 res, err := c.Get(url)22 if err != nil {23 return errMsg(err)23 return errMsg{err}24 }25 return statusMsg(res.StatusCode)26}2728type statusMsg int29type errMsg error2930type errMsg struct{ err error }3132// For messages that contain errors it's often handy to also implement the33// error interface on the message.34func (e errMsg) Error() string { return e.err.Error() }3536func initialize() (tea.Model, tea.Cmd) {37 return model{}, checkServer38}39
42 unmodified lines
modified:tutorials/commands/README.md+7 added-2 removed
63 unmodified lines
6465 if err != nil {66 // There was an error making our request. Wrap the error we received67 // in a message and return it.68 return errMsg(err)68 return errMsg{err}69 }70 // We received a response from the server. Return the HTTP status code71 // as a message.72 return statusMsg(res.StatusCode)73 }7475 type statusMsg int76 type errMsg error7677 type errMsg struct{ err error }7879 // For messages that contain errors it's often handy to also implement the80 // error interface on the message.81 func (e errMsg) Error() string { return e.err.Error() }82```8384And notice that we've defined two new `Msg` types. They can be any type, even85an empty struct. We'll come back to them later later in our update function.
161 unmodified lines