Skip to content

[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

Merged
merged 1 commit into from
May 25, 2025

Conversation

mohamed-essam
Copy link
Collaborator

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

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • Extended the README / documentation, if necessary

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

@mlsmaycon mlsmaycon requested a review from Copilot May 22, 2025 14:25
Copy link
Contributor

@Copilot Copilot AI left a 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)

@mohamed-essam mohamed-essam force-pushed the fix/management-rest-panic branch from 424fdb8 to 66a2875 Compare May 25, 2025 14:01
@mohamed-essam mohamed-essam force-pushed the fix/management-rest-panic branch from 66a2875 to 2b08885 Compare May 25, 2025 14:02
Copy link

@mohamed-essam mohamed-essam requested a review from mlsmaycon May 25, 2025 14:53
@mlsmaycon mlsmaycon requested a review from Copilot May 25, 2025 14:56
Copy link
Contributor

@Copilot Copilot AI left a 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 outer err in NewRequest.
  • 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 of parseResponse 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)

@mlsmaycon mlsmaycon merged commit 670446d into main May 25, 2025
35 checks passed
@mlsmaycon mlsmaycon deleted the fix/management-rest-panic branch May 25, 2025 14:57
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