Skip to content

How can I return 4XX error response #40

Answered by Tochemey
Tochemey asked this question in Support
Discussion options

You must be logged in to vote

So I think the actual CheckStatus should be something like this:

// CheckStatus validates the response has an acceptable status code.
func CheckStatus(acceptStatuses ...int) requests.ResponseHandler {
	return func(res *http.Response) error {
		for _, code := range acceptStatuses {
			if res.StatusCode == code {
				return nil
			}
		}
		// cast error into error response
		se := (*requests.ResponseError)(res)
		// read the error response body
		var buf strings.Builder
		_, copyErr := io.Copy(&buf, se.Body)
		if copyErr == nil {
			return errors.New(buf.String())
		}
		return copyErr
	}
}

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@Tochemey
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by Tochemey
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants