mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
Error msg should be a struct in command tutorial, not a generic error
This commit is contained in:
@@ -65,7 +65,7 @@ result as a `Msg`.
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
// There was an error making our request. Wrap the error we received
|
// There was an error making our request. Wrap the error we received
|
||||||
// in a message and return it.
|
// in a message and return it.
|
||||||
return errMsg(err)
|
return errMsg{err}
|
||||||
}
|
}
|
||||||
// We received a response from the server. Return the HTTP status code
|
// We received a response from the server. Return the HTTP status code
|
||||||
// as a message.
|
// as a message.
|
||||||
@@ -73,7 +73,12 @@ result as a `Msg`.
|
|||||||
}
|
}
|
||||||
|
|
||||||
type statusMsg int
|
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
|
And notice that we've defined two new `Msg` types. They can be any type, even
|
||||||
|
|||||||
@@ -20,13 +20,18 @@ func checkServer() tea.Msg {
|
|||||||
c := &http.Client{Timeout: 10 * time.Second}
|
c := &http.Client{Timeout: 10 * time.Second}
|
||||||
res, err := c.Get(url)
|
res, err := c.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errMsg(err)
|
return errMsg{err}
|
||||||
}
|
}
|
||||||
return statusMsg(res.StatusCode)
|
return statusMsg(res.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
type statusMsg int
|
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) {
|
func initialize() (tea.Model, tea.Cmd) {
|
||||||
return model{}, checkServer
|
return model{}, checkServer
|
||||||
|
|||||||
Reference in New Issue
Block a user