Answer the question
In order to leave comments, you need to log in
How to handle erroneous API response to go?
How to correctly handle an erroneous response received from the API?
For example, with a correct request, a correct json model is returned, let's call it User, which has a structure in Go:
type User struct {
Country string `json:"country"`
Login string `json:"login"`
DisplayName string `json:"display_name"`
UID string `json:"uid"`
}
type Error struct {
Message string `json:"message"`
Description string `json:"description"`
Error string `json:"error"`
}
func (c *Client) GetUser(login string) (User,error) {
// ..body
}
Answer the question
In order to leave comments, you need to log in
I thought in the Error structure to implement the error interface and pass an erroneous response directly through error.Quite right.
func(e Error) Error() string{
return e.Message
}
type Error struct {
Message string `json:"message"`
Description string `json:"description"`
Error string `json:"error"`
error
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question