diff --git a/openml/_api/clients/http.py b/openml/_api/clients/http.py index cb0cf4b15..f18bb6de5 100644 --- a/openml/_api/clients/http.py +++ b/openml/_api/clients/http.py @@ -453,14 +453,17 @@ def _validate_response( code, message = self._parse_exception_response(response) except (requests.exceptions.JSONDecodeError, xml.parsers.expat.ExpatError) as e: + extra = f"Status code: {response.status_code}\n{response.text}" + parse_error = OpenMLServerError( + f"Unexpected server error when calling {url}. Please contact the " + f"developers!\n{extra}" + ) + if method != "GET": - extra = f"Status code: {response.status_code}\n{response.text}" - raise OpenMLServerError( - f"Unexpected server error when calling {url}. Please contact the " - f"developers!\n{extra}" - ) from e + raise parse_error from e - exception = e + parse_error.__cause__ = e + exception = parse_error except Exception as e: # If we failed to parse it out, diff --git a/tests/test_api/test_http.py b/tests/test_api/test_http.py index 570c62e62..c4459ce1b 100644 --- a/tests/test_api/test_http.py +++ b/tests/test_api/test_http.py @@ -6,7 +6,7 @@ from pathlib import Path from urllib.parse import urljoin, urlparse from openml.enums import APIVersion -from openml.exceptions import OpenMLAuthenticationError +from openml.exceptions import OpenMLAuthenticationError, OpenMLServerError from openml._api import HTTPClient, HTTPCache import openml @@ -160,10 +160,31 @@ def test_get_with_api_key(http_client, sample_path, test_apikey_v1): @pytest.mark.test_server() def test_get_without_api_key_raises(http_client): - with openml.config.overwrite_config_context({"apikey": None}), pytest.raises(OpenMLAuthenticationError): + with openml.config.overwrite_config_context({"apikey": None}), pytest.raises( + OpenMLAuthenticationError + ): http_client.get("task/1", use_api_key=True) +def test_get_with_invalid_error_xml_shows_response(http_client, sample_url_v1): + response = Response() + response.status_code = 500 + response._content = b"