-
-
Notifications
You must be signed in to change notification settings - Fork 707
[management/client/rest] Fix panic on unknown errors #3865
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a panic when the response from the server is either empty or not in the expected format by improving error handling in the client’s HTTP response parsing.
- Update error propagation in NewRequest to return the parsed error instead of a generic error.
- Modify the error messages in parseResponse to return a formatted error using the HTTP status code.
- Add a new test to verify that a 404 error is handled correctly without panicking.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
management/client/rest/client.go | Update error propagation and error message format in HTTP response parsing |
management/client/rest/accounts_test.go | Add test to verify handling of connection errors (404) |
424fdb8
to
66a2875
Compare
66a2875
to
2b08885
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a panic when the REST client encounters responses without a valid body by returning detailed parsing errors and adds a test for a 404 scenario.
- Return the correct parsing error (
pErr
) instead of the outererr
inNewRequest
. - Improve
parseResponse
to handle missing bodies and JSON unmarshal failures with formatted errors. - Add a test to cover an HTTP 404 response in
Accounts.List
.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
management/client/rest/client.go | Correct error return in NewRequest and enhance parseResponse error handling |
management/client/rest/accounts_test.go | Introduce TestAccounts_List_ConnErr to assert behavior on 404 responses |
Comments suppressed due to low confidence (4)
management/client/rest/client.go:147
- Add
defer resp.Body.Close()
at the start ofparseResponse
to ensure the response body is always closed and prevent resource leaks.
func parseResponse[T any](resp *http.Response) (T, error) {
management/client/rest/accounts_test.go:69
- [nitpick] The test name suggests a connection error but it actually asserts a 404 response. Consider renaming it to
TestAccounts_List_404Error
for clarity.
func TestAccounts_List_ConnErr(t *testing.T) {
management/client/rest/accounts_test.go:73
- [nitpick] Consider adding assertions on the full error message format and adding tests for missing-body and malformed-JSON scenarios to cover the new error branches in
parseResponse
.
assert.Contains(t, err.Error(), "404")
management/client/rest/client.go:150
- [nitpick] For consistency and readability, consider rephrasing to
HTTP Error %d: body missing
to match common formatting conventions for HTTP errors.
return ret, fmt.Errorf("Body missing, HTTP Error code %d", resp.StatusCode)
Describe your changes
Fixes panic when response has no body or body does not conform to
management/server/http/util.(ErrorResponse)
Issue ticket number and link
Stack
Checklist