Error handling #579
-
Hi, I struggle to understand who error handling is working using Resty. I've tried to use resp.IsError() without success. I've read some similar topic here which uses the SetError method on a struct (Error or Response depending on the case), but this method is maybe specifically tied to a marshalling error when using the SetResult method. Do you have king of a simple example to understand the good way to handle error using Resty? Best. This question has been addressed 2 months ago in the issue #560 but maybe it wasn't the good place to post that why I put it here. func PutItems( items []string) ([]string, string, rest_errors.RestErr){
resp, err := client.R().
SetHeader("Accept", "application/json").
SetBody(myBody).
Put(myEndpoint)
if err != nil { // 404 don't happen here apparently
return nil, rest_errors.NewBadRequestError("API Gateway - http update user cars:" + err.Error())
}
if resp.IsError() { // 404 is not detected either here
return nil, errors.New("Unknow error here")
}
var createdItems []string
err2 := json.Unmarshal(resp.Body(), &createdItems)
if err2 != nil {
return nil, errors.New("error when unmarshalling items" + err2.Error())
}
return createdItems, nil
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @BigBoulard, Disclaimer: I would answer this from my project experienced before There is 3 layer validation for the response from resty client:
In your case I would suggest you try to validate the HTTP status code if the statusCode
I Hope this could be help this discussion. |
Beta Was this translation helpful? Give feedback.
Hi @BigBoulard,
Disclaimer: I would answer this from my project experienced before
There is 3 layer validation for the response from resty client:
resp.StatusCode()
(I would use this for this case)json.Unmarshal(resp.Body(), &yourStruct)
In your case I would suggest you try to validate the HTTP status code if the statusCode
404
: