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

This commit is contained in:
Christian Rocha 2020-08-26 15:28:18 -04:00
parent a2a85d3c73
commit 70e94cffae
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
2 changed files with 14 additions and 4 deletions

View File

@ -65,7 +65,7 @@ result as a `Msg`.
if err != nil {
// There was an error making our request. Wrap the error we received
// in a message and return it.
return errMsg(err)
return errMsg{err}
}
// We received a response from the server. Return the HTTP status code
// as a message.
@ -73,7 +73,12 @@ result as a `Msg`.
}
type statusMsg int
type errMsg error
type errMsg struct{ err error }
// For messages that contain errors it's often handy to also implement the
// error interface on the message.
func (e errMsg) Error() string { return e.err.Error() }
```
And notice that we've defined two new `Msg` types. They can be any type, even

View File

@ -20,13 +20,18 @@ func checkServer() tea.Msg {
c := &http.Client{Timeout: 10 * time.Second}
res, err := c.Get(url)
if err != nil {
return errMsg(err)
return errMsg{err}
}
return statusMsg(res.StatusCode)
}
type statusMsg int
type errMsg error
type errMsg struct{ err error }
// For messages that contain errors it's often handy to also implement the
// error interface on the message.
func (e errMsg) Error() string { return e.err.Error() }
func initialize() (tea.Model, tea.Cmd) {
return model{}, checkServer