Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLA-355 - Validating errors ( 400, 401, 404 ) #10

Merged
merged 3 commits into from
Nov 5, 2022

Conversation

rhvivancoeffio
Copy link
Contributor

Proposed Changes

Validating http errors

@rhvivancoeffio rhvivancoeffio requested a review from a team as a code owner November 4, 2022 17:55
@linear
Copy link

linear bot commented Nov 4, 2022

PLA-355 Internal errors for invalid AutoPi unit ids

We're seeing 500s from devices-api for GET /v1/autopi/unit/8396b1a8-79bc-ea66-2a9c-4ec75af78ce4. Ingress query.

Acceptance Criteria:

  • If the AutoPi api returns a 404, we should return a 404 back to the client, e.g., fiber.ErrNotFound
  • A unit test around the above.

In the past we had aimed to accomplish this but it seems like the client code was not quite right.

Comment on lines +83 to +86
errResp := BuildResponseError(res.StatusCode, errors.Errorf("received non success status code %d with body: %s", res.StatusCode, string(body)))
if res.StatusCode == 400 || res.StatusCode == 401 || res.StatusCode == 404 {
// unrecoverable since probably bad payload or n
return retry.Unrecoverable(errResp)
return retry.Unrecoverable(BuildResponseError(res.StatusCode, errResp))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): Wrapping this twice seems strange to me. What if we did something like

type HTTPResponseError struct {
	StatusCode int
	Body       []byte
}

func (e *HTTPResponseError) Error() string {
	return fmt.Sprintf("status code %d with body %s", e.StatusCode, e.Body)
}

created that, and wrapped if the status code indicates unrecoverable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: It might also help the discussion here to talk about how you'd want to use this new behavior of HTTPClientWrapper to pass along 404s. Right now it seems like you have to search for the string 404.

Comment on lines 97 to 98
if _, ok := err.(retry.Error); ok {
retryErrors := err.(retry.Error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I think we can bring this down to one line:

Suggested change
if _, ok := err.(retry.Error); ok {
retryErrors := err.(retry.Error)
if retryErrors, ok := err.(retry.Error); ok {

if _, ok := err.(retry.Error); ok {
retryErrors := err.(retry.Error)
for _, e := range retryErrors {
if httpError, ok := e.(HTTPResponseError); ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: This library is interesting and I hadn't looked at it closely before—if it's not nil the error here is a list of length 5 with all the errors returned. That is cool. I wonder if this is the right thing to do when, for example, we get a 500 and then later in the retry process a transport-level error.

I guess it's hard to summarize five different errors. What's here is good.

@@ -21,7 +21,7 @@ func Test_httpClientWrapper_ExecuteRequest_failsTooManyRetries(t *testing.T) {

assert.Equal(t, 5, c, "expected five retries")
assert.Error(t, err, "expected error")
assert.Contains(t, err.Error(), "All attempts fail")
//assert.Contains(t, myErr, "All attempts fail")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Can we check that the returned error mentions the status code and contains the body?

.gitignore Outdated
@@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
*.idea
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Add a newline at the end here.

@rhvivancoeffio rhvivancoeffio merged commit 051e28d into main Nov 5, 2022
@rhvivancoeffio rhvivancoeffio deleted the shared-http-client-custom-error branch November 5, 2022 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants