Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated error message to include server side errors if present. #183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/kagglehub/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def kaggle_api_raise_for_status(response: requests.Response, resource_handle: Op
response.raise_for_status()
except requests.HTTPError as e:
message = str(e)
server_error_message = response.json().get("message", "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess some response are not json format so it fails some integration tests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

if server_error_message:
server_error_message = f"The server reported the following issues: {server_error_message}\n"
resource_url = resource_handle.to_url() if resource_handle else response.url
if response.status_code in {HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN}:
if isinstance(resource_handle, CompetitionHandle):
Expand All @@ -72,17 +75,19 @@ def kaggle_api_raise_for_status(response: requests.Response, resource_handle: Op
message = (
f"{response.status_code} Client Error."
"\n\n"
f"You don't have permission to access resource at URL: {resource_url}"
"\nPlease make sure you are authenticated if you are trying to access a"
" private resource or a resource requiring consent."
f"You don't have permission to access resource at URL: {resource_url}. "
f"{server_error_message}"
f"Please make sure you are authenticated if you are trying to access a "
f"private resource or a resource requiring consent."
)

if response.status_code == HTTPStatus.NOT_FOUND:
message = (
f"{response.status_code} Client Error."
"\n\n"
f"Resource not found at URL: {resource_url}"
"\nPlease make sure you specified the correct resource identifiers."
f"Resource not found at URL: {resource_url}\n"
f"{server_error_message}"
"Please make sure you specified the correct resource identifiers."
)

# Default handling
Expand Down
4 changes: 2 additions & 2 deletions tests/server_stubs/kaggle_api_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def head() -> ResponseReturnValue:

@app.errorhandler(404)
def error(e: Exception): # noqa: ANN201
data = {"code": "404", "error": str(e)}
return jsonify(data), 200
data = {"code": "404", "error": str(e), "message": "server side error"}
return jsonify(data), 404


@app.route("/api/v1/no-integrity", methods=["GET"])
Expand Down
8 changes: 7 additions & 1 deletion tests/test_kaggle_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import kagglehub
from kagglehub import clients
from kagglehub.clients import KaggleApiV1Client
from kagglehub.exceptions import DataCorruptionError
from kagglehub.exceptions import DataCorruptionError, KaggleApiHTTPError
from tests.fixtures import BaseTestCase

from .server_stubs import kaggle_api_stub as stub
Expand Down Expand Up @@ -68,6 +68,12 @@ def test_download_corrupted_file_fail_integrity_check(self) -> None:
# Assert the corrupted file has been deleted.
self.assertFalse(os.path.exists(out_file))

def test_error_message(self) -> None:
api_client = KaggleApiV1Client()
with self.assertRaises(KaggleApiHTTPError) as ex:
api_client.get("/error")
self.assertIn("The server reported the following issues:", str(ex.exception))

@patch.dict("os.environ", {})
def test_get_user_agent(self) -> None:
self.assertEqual(clients.get_user_agent(), f"kagglehub/{kagglehub.__version__}")
Expand Down