Handling failures in Init #623
Unanswered
ZeroDeltaAlpha
asked this question in
Q&A
Replies: 1 comment 3 replies
-
You can have a type HttpResponse struct {
data []byte // change this to your struct if you want, or make your struct include the error
err error
}
func FetchData(url string) tea.Msg {
resp, err := http.Get("http://example.com/")
if err != nil {
return HttpResponse{[]byte{}, err}
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
return HttpResponse{body, err}
} I hope that helps! |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi I'm writing an app that does data fetching in the init func using http.
The data fetching func returns a Struct and a error type, in the case of a failure I send back an empty Struct and an err.
i run this using tea.Sequence, what is the best way to handle if err is not nil on init and take appropriate action ?
Beta Was this translation helpful? Give feedback.
All reactions