Skip to content

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

Merged
parthea merged 1 commit into
googleapis:mainfrom
cyphercodes:fix-api-core-list-error-payload-18223
Aug 28, 2026
Merged

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

Conversation

@cyphercodes

Copy link
Copy Markdown
Contributor

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 = {}

@parthea
parthea enabled auto-merge (squash) August 28, 2026 16:44
@parthea parthea added kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests. labels Aug 28, 2026
@parthea parthea self-assigned this Aug 28, 2026
@parthea
parthea merged commit 02d1fd8 into googleapis:main Aug 28, 2026
44 of 45 checks passed
daniel-sanche pushed a commit to daniel-sanche/google-cloud-python that referenced this pull request Aug 28, 2026
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:
- [x] Make sure to open an issue as a
[bug/issue](https://github.com/googleapis/google-cloud-python/issues)
before writing your code! That way we can discuss the change, evaluate
designs, and agree on the general idea
- [x] Ensure the tests and linter pass
- [x] Code coverage does not decrease (if any source code was changed)
- [x] Appropriate docs were updated (if necessary; no docs change needed
for this bug fix)

Fixes googleapis#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.

Co-authored-by: cyphercodes <cyphercodes@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kokoro:force-run Add this label to force Kokoro to re-run the tests. kokoro:run Add this label to force Kokoro to re-run the tests.

Projects

None yet

3 participants