Skip to content

google-api-core: non-object REST error members mask HTTP failures with AttributeError #19253

Backlog

Description

@pradeep-ramola

Package

google-api-core, reproduced from google-cloud-python main at a4bec90 using Python 3.11 and a local requests.Response (no network or credentials).

Reproduction

import json
import requests
from google.api_core import exceptions

response = requests.Response()
response.status_code = 503
response._content = json.dumps({"error": None}).encode()
response.request = requests.Request("GET", "https://example.com/").prepare()

error = exceptions.from_http_response(response)

Actual result:

AttributeError: 'NoneType' object has no attribute 'get'

The crash occurs at payload.get("error", {}).get("message", "unknown error") in format_http_response_error. Non-object error members (such as a string) and non-object top-level JSON values also trigger attribute errors.

Expected

Error decoding should return the exception selected by the HTTP status (ServiceUnavailable for 503 / TooManyRequests for 429), with the original response attached and the existing unknown error fallback when a standard error object is unavailable. An error-envelope shape should not replace a retryable HTTP failure with AttributeError.

Proposed Scope

Normalize the top-level payload and its error member to mappings before reading standard fields. Preserve existing valid dictionary payloads, structured details, and the list-wrapper behavior added in #18232. This issue concerns non-object payloads / error members, not the already-fixed list-wrapper case.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions