1
+ import logging
1
2
from http import HTTPStatus
2
3
from typing import Any , Optional
3
4
4
5
import requests
5
6
6
7
from kagglehub .handle import CompetitionHandle , ResourceHandle
8
+ from kagglehub .logger import EXTRA_CONSOLE_BLOCK
9
+
10
+ logger = logging .getLogger (__name__ )
7
11
8
12
9
13
class CredentialError (Exception ):
@@ -58,6 +62,13 @@ def kaggle_api_raise_for_status(response: requests.Response, resource_handle: Op
58
62
response .raise_for_status ()
59
63
except requests .HTTPError as e :
60
64
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 })
61
72
resource_url = resource_handle .to_url () if resource_handle else response .url
62
73
if response .status_code in {HTTPStatus .UNAUTHORIZED , HTTPStatus .FORBIDDEN }:
63
74
if isinstance (resource_handle , CompetitionHandle ):
@@ -72,17 +83,19 @@ def kaggle_api_raise_for_status(response: requests.Response, resource_handle: Op
72
83
message = (
73
84
f"{ response .status_code } Client Error."
74
85
"\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."
78
90
)
79
91
80
92
if response .status_code == HTTPStatus .NOT_FOUND :
81
93
message = (
82
94
f"{ response .status_code } Client Error."
83
95
"\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."
86
99
)
87
100
88
101
# Default handling
0 commit comments