@@ -12,7 +12,7 @@ class RepositoryNotFoundError(HTTPError):
1212
1313 ```py
1414 >>> from huggingface_hub import model_info
15- >>> model_info("<non_existant_repository >")
15+ >>> model_info("<non_existent_repository >")
1616 huggingface_hub.utils._errors.RepositoryNotFoundError: 404 Client Error: Repository Not Found for url: <url>
1717 ```
1818 """
@@ -30,7 +30,7 @@ class RevisionNotFoundError(HTTPError):
3030
3131 ```py
3232 >>> from huggingface_hub import hf_hub_download
33- >>> hf_hub_download('bert-base-cased', 'config.json', revision='<non-existant -revision>')
33+ >>> hf_hub_download('bert-base-cased', 'config.json', revision='<non-existent -revision>')
3434 huggingface_hub.utils._errors.RevisionNotFoundError: 404 Client Error: Revision Not Found for url: <url>
3535 ```
3636 """
@@ -48,7 +48,7 @@ class EntryNotFoundError(HTTPError):
4848
4949 ```py
5050 >>> from huggingface_hub import hf_hub_download
51- >>> hf_hub_download('bert-base-cased', '<non-existant -file>')
51+ >>> hf_hub_download('bert-base-cased', '<non-existent -file>')
5252 huggingface_hub.utils._errors.EntryNotFoundError: 404 Client Error: Entry Not Found for url: <url>
5353 ```
5454 """
@@ -101,6 +101,26 @@ def _add_request_id_to_error_args(e, request_id):
101101 e .args = (e .args [0 ] + f" (Request ID: { request_id } )" ,) + e .args [1 :]
102102
103103
104+ def _add_server_message_to_error_args (e : HTTPError , response : Response ):
105+ """
106+ If the server response raises an HTTPError, we try to decode the response body and
107+ find an error message. If the server returned one, it is added to the HTTPError
108+ message.
109+ """
110+ try :
111+ server_message = response .json ().get ("error" , None )
112+ except JSONDecodeError :
113+ return
114+
115+ if (
116+ server_message is not None
117+ and len (server_message ) > 0
118+ and len (e .args ) > 0
119+ and isinstance (e .args [0 ], str )
120+ ):
121+ e .args = (e .args [0 ] + "\n \n " + str (server_message ),) + e .args [1 :]
122+
123+
104124def _raise_for_status (response ):
105125 """
106126 Internal version of `response.raise_for_status()` that will refine a
@@ -144,6 +164,7 @@ def _raise_for_status(response):
144164 e = RepositoryNotFoundError (message , response )
145165
146166 _add_request_id_to_error_args (e , request_id )
167+ _add_server_message_to_error_args (e , response )
147168
148169 raise e
149170
0 commit comments