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

Update error handling in DoJSON #722

Merged
merged 2 commits into from
Jun 20, 2023

Conversation

brandonc
Copy link
Collaborator

@brandonc brandonc commented Jun 16, 2023

This was part of a new method in #717 -- when an error occurs, the resp.Body is nil so we need to wait until after checking err != nil

@brandonc brandonc requested a review from a team as a code owner June 16, 2023 17:58
sebasslash
sebasslash previously approved these changes Jun 16, 2023
@brandonc
Copy link
Collaborator Author

brandonc commented Jun 16, 2023

@sebasslash I added some unit tests

Copy link
Contributor

@sebasslash sebasslash left a comment

Choose a reason for hiding this comment

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

I think this looks good. Some stylistic non-blocking items ⬇️

Comment on lines +36 to +63
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fakeBody := map[string]any{
"id": "example",
"name": "fixture",
"method": r.Method,
}
fakeBodyRaw, err := json.Marshal(fakeBody)
require.NoError(t, err)

if strings.HasSuffix(r.URL.String(), "/ok_request") {
w.Header().Set("content-type", "application/json")
w.Header().Set("content-length", strconv.FormatInt(int64(len(fakeBodyRaw)), 10))
w.WriteHeader(http.StatusOK)
_, err = w.Write(fakeBodyRaw)
require.NoError(t, err)
} else if strings.HasSuffix(r.URL.String(), "/bad_request") {
w.WriteHeader(http.StatusBadRequest)
} else if strings.HasSuffix(r.URL.String(), "/created_request") {
w.WriteHeader(http.StatusCreated)
} else if strings.HasSuffix(r.URL.String(), "/not_modified_request") {
w.WriteHeader(http.StatusNotModified)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(func() {
testServer.Close()
})
Copy link
Contributor

Choose a reason for hiding this comment

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

💅 Worth defining this as a helper in helper_test.go?

Comment on lines +45 to +59
if strings.HasSuffix(r.URL.String(), "/ok_request") {
w.Header().Set("content-type", "application/json")
w.Header().Set("content-length", strconv.FormatInt(int64(len(fakeBodyRaw)), 10))
w.WriteHeader(http.StatusOK)
_, err = w.Write(fakeBodyRaw)
require.NoError(t, err)
} else if strings.HasSuffix(r.URL.String(), "/bad_request") {
w.WriteHeader(http.StatusBadRequest)
} else if strings.HasSuffix(r.URL.String(), "/created_request") {
w.WriteHeader(http.StatusCreated)
} else if strings.HasSuffix(r.URL.String(), "/not_modified_request") {
w.WriteHeader(http.StatusNotModified)
} else {
w.WriteHeader(http.StatusNotFound)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💅 A switch/case may be cleaner given the longer if/else chain:

paths := strings.Split(r.URL.Path, "/")
switch paths[len(paths) - 1] {
  case "ok_request":
     ...
}

@brandonc brandonc merged commit 6dc251e into main Jun 20, 2023
@brandonc brandonc deleted the brandonc/dont_close_body_if_err_not_nil branch June 20, 2023 15:18
@github-actions
Copy link

Reminder to the contributor that merged this PR: if your changes have added important functionality or fixed a relevant bug, open a follow-up PR to update CHANGELOG.md with a note on your changes.

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.

2 participants