GoogleCloudPlatform / cloud-sql-python-connector

A Python library for connecting securely to your Cloud SQL instances.
Apache License 2.0
284 stars 67 forks source link

Improve `aiohttp` error handling by converting response body to error #477

Open jackwotherspoon opened 2 years ago

jackwotherspoon commented 2 years ago

The Python Connector currently uses raise_for_status=True when using aiohttp's ClientSession.

The default behaviour for this argument is to close http connections when response status code > 400 and raise generic error message (ex. 400 Bad Request), so the response body which may have a more helpful error message or instructions cannot be accessed.

It is recommended to not use raise for status if access to the errors response body is required. Because of this the Python Connector should do something along the following (pseudocode):


def convert_response(resp):
  # attempt to read response and format custom error
  try: 
    resp_body = await resp.json()
    # ... raise error using standard response format
    build_error(resp.status, resp_body)
  # if error occurs reading response body, fallback to raise_for_status 
  except:
    resp.raise_for_status()

# --------------------------------------------------------------------
# ... inside _get_metadata (refresh_utils.py)
async with client_session.get(url, headers=headers) as resp:
  # if not 2XX status code
  if not resp.ok:
    # handle standard response and format it into error
    convert_response(resp)
  # ... continue onwards
jackwotherspoon commented 4 months ago

Improve error messages around IAM as well