1+ import logging
12from http import HTTPStatus
23from typing import Any , Optional
34
45import requests
56
67from kagglehub .handle import CompetitionHandle , ResourceHandle
8+ from kagglehub .logger import EXTRA_CONSOLE_BLOCK
9+
10+ logger = logging .getLogger (__name__ )
711
812
913class CredentialError (Exception ):
@@ -58,6 +62,13 @@ def kaggle_api_raise_for_status(response: requests.Response, resource_handle: Op
5862 response .raise_for_status ()
5963 except requests .HTTPError as e :
6064 message = str (e )
65+ server_error_message = ""
66+ try :
67+ server_error_message = response .json ().get ("message" , "" )
68+ if server_error_message :
69+ server_error_message = f"The server reported the following issues: { server_error_message } \n "
70+ except requests .exceptions .JSONDecodeError as ex :
71+ logger .info (f"Server payload is not json. See { ex } " , extra = {** EXTRA_CONSOLE_BLOCK })
6172 resource_url = resource_handle .to_url () if resource_handle else response .url
6273 if response .status_code in {HTTPStatus .UNAUTHORIZED , HTTPStatus .FORBIDDEN }:
6374 if isinstance (resource_handle , CompetitionHandle ):
@@ -72,17 +83,19 @@ def kaggle_api_raise_for_status(response: requests.Response, resource_handle: Op
7283 message = (
7384 f"{ response .status_code } Client Error."
7485 "\n \n "
75- f"You don't have permission to access resource at URL: { resource_url } "
76- "\n Please make sure you are authenticated if you are trying to access a"
77- " private resource or a resource requiring consent."
86+ f"You don't have permission to access resource at URL: { resource_url } . "
87+ f"{ server_error_message } "
88+ f"Please make sure you are authenticated if you are trying to access a "
89+ f"private resource or a resource requiring consent."
7890 )
7991
8092 if response .status_code == HTTPStatus .NOT_FOUND :
8193 message = (
8294 f"{ response .status_code } Client Error."
8395 "\n \n "
84- f"Resource not found at URL: { resource_url } "
85- "\n Please make sure you specified the correct resource identifiers."
96+ f"Resource not found at URL: { resource_url } \n "
97+ f"{ server_error_message } "
98+ "Please make sure you specified the correct resource identifiers."
8699 )
87100
88101 # Default handling
0 commit comments