From 70e94cffae4c72909ab47cdb28dfd2d69223bb45 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Wed, 26 Aug 2020 15:28:18 -0400 Subject: [PATCH] Error msg should be a struct in command tutorial, not a generic error --- tutorials/commands/README.md | 9 +++++++-- tutorials/commands/main.go | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tutorials/commands/README.md b/tutorials/commands/README.md index 7d5e8ff..6fcafba 100644 --- a/tutorials/commands/README.md +++ b/tutorials/commands/README.md @@ -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 diff --git a/tutorials/commands/main.go b/tutorials/commands/main.go index 4fee2bc..6a601ca 100644 --- a/tutorials/commands/main.go +++ b/tutorials/commands/main.go @@ -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