Skip to content

fix(api-core): handle list-shaped REST error payloads - #18232

Open
cyphercodes wants to merge 1 commit into
googleapis:mainfrom
cyphercodes:fix-api-core-list-error-payload-18223
Open

fix(api-core): handle list-shaped REST error payloads#18232
cyphercodes wants to merge 1 commit into
googleapis:mainfrom
cyphercodes:fix-api-core-list-error-payload-18223

Conversation

@cyphercodes

Copy link
Copy Markdown

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary; no docs change needed for this bug fix)

Fixes #18223 🦕

Summary

  • Normalize list-shaped REST error payloads to the first dict item before reading the standard error object.
  • Preserve existing dict payload behavior and fall back to unknown error when no dict item is present.
  • Add unit coverage for list-wrapped error payloads.

Tests

  • pytest -q tests/unit/test_exceptions.py -k 'http_response or error_details_from_rest_response'
  • pytest -q tests/unit/test_exceptions.py
  • ruff format --check --target-version=py310 --line-length=88 google/api_core/exceptions.py tests/unit/test_exceptions.py
  • flake8 google/api_core/exceptions.py tests/unit/test_exceptions.py
  • git diff --check

CLA

Google CLA is required by the repository; awaiting the repository's CLA check on this PR.

@cyphercodes
cyphercodes requested a review from a team as a code owner August 27, 2026 03:48

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the format_http_response_error function in exceptions.py to handle cases where the HTTP response payload is a list, extracting the first dictionary item from the list to parse the error. It also adds corresponding unit tests. The reviewer suggested ensuring that payload is explicitly validated as a dictionary to prevent potential AttributeError crashes if the payload is of another non-dictionary type, which aligns with defensive programming guidelines.

Comment on lines +513 to 515
if isinstance(payload, list):
payload = next((item for item in payload if isinstance(item, dict)), {})
payload = {} if not payload else payload

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To ensure robust defensive programming against untrusted or malformed REST payloads, we should guarantee that payload is a dictionary before calling .get() on it. If the payload is a string, number, or other non-dictionary type, the current implementation will raise an AttributeError. Replacing the falsy check with an explicit isinstance(payload, dict) check prevents potential crashes.

Suggested change
if isinstance(payload, list):
payload = next((item for item in payload if isinstance(item, dict)), {})
payload = {} if not payload else payload
if isinstance(payload, list):
payload = next((item for item in payload if isinstance(item, dict)), {})
if not isinstance(payload, dict):
payload = {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant