diff --git a/src/auth/src/supabase_auth/helpers.py b/src/auth/src/supabase_auth/helpers.py index ab110a534..cdac7e8a6 100644 --- a/src/auth/src/supabase_auth/helpers.py +++ b/src/auth/src/supabase_auth/helpers.py @@ -137,11 +137,19 @@ def handle_exception(error: HTTPStatusError | RuntimeError) -> AuthError: return AuthRetryableError(get_error_message(error), 0) try: network_error_codes = [502, 503, 504, 520, 521, 522, 523, 524, 530] - if error.response.status_code in network_error_codes: - return AuthRetryableError( - get_error_message(error), error.response.status_code - ) - data = error.response.json() + status = error.response.status_code + try: + data = error.response.json() + except Exception: + if status in network_error_codes: + # The reason phrase is empty when the server sends none and + # the status is non-standard (e.g. Cloudflare's 520-530). + return AuthRetryableError( + error.response.reason_phrase or f"HTTP {status}", status + ) + raise + if status in network_error_codes: + return AuthRetryableError(get_error_message(data), status) error_code = None response_api_version = parse_response_api_version(error.response) diff --git a/src/auth/tests/test_helpers.py b/src/auth/tests/test_helpers.py index f9a319090..76040f6f5 100644 --- a/src/auth/tests/test_helpers.py +++ b/src/auth/tests/test_helpers.py @@ -195,20 +195,64 @@ def test_model_dump_json_pydantic_v1() -> None: mock_model.json.assert_called_once() -def test_handle_exception_network_error() -> None: - # Test case for network errors (502, 503, 504) - mock_response = MagicMock(spec=Response) - mock_response.status_code = 503 +def _network_error(response: Response) -> HTTPStatusError: + response.request = httpx.Request("POST", f"{TEST_URL}/token") + return HTTPStatusError("Server error", request=response.request, response=response) + + +@pytest.mark.parametrize("status", [502, 503, 504, 520, 521, 522, 523, 524, 530]) +def test_handle_exception_network_error(status: int) -> None: + exception = _network_error(Response(status, json={"msg": "upstream unavailable"})) + + result = handle_exception(exception) + + assert isinstance(result, AuthRetryableError) + assert result.message == "upstream unavailable" + assert result.status == status + + +@pytest.mark.parametrize("key", ["msg", "message"]) +def test_handle_exception_network_error_preserves_json_message(key: str) -> None: + exception = _network_error(Response(503, json={key: "upstream unavailable"})) - exception = HTTPStatusError( - "Network error", request=MagicMock(), response=mock_response - ) result = handle_exception(exception) assert isinstance(result, AuthRetryableError) + assert result.message == "upstream unavailable" assert result.status == 503 +def test_handle_exception_network_error_non_json_uses_reason_phrase() -> None: + exception = _network_error(Response(502, text="Bad Gateway")) + + result = handle_exception(exception) + + assert isinstance(result, AuthRetryableError) + assert result.message == "Bad Gateway" + assert result.status == 502 + + +@pytest.mark.parametrize( + "response", + [ + # Server sent an empty reason phrase. + Response(503, text="oops", extensions={"reason_phrase": b""}), + # Cloudflare codes have no standard reason phrase. + Response(520, text="oops"), + ], +) +def test_handle_exception_network_error_non_json_without_reason_phrase( + response: Response, +) -> None: + exception = _network_error(response) + + result = handle_exception(exception) + + assert isinstance(result, AuthRetryableError) + assert result.message == f"HTTP {response.status_code}" + assert result.status == response.status_code + + def test_handle_exception_with_weak_password_attribute() -> None: # Test case with error_code=None and no weak_password object, so the # generic AuthApiError path is taken.