Skip to content

Commit 00f12e5

Browse files
authored
Whoami: custom message only on unauthorized (#3288)
1 parent aa28404 commit 00f12e5

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/huggingface_hub/hf_api.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,18 +1781,20 @@ def whoami(self, token: Union[bool, str, None] = None) -> Dict:
17811781
try:
17821782
hf_raise_for_status(r)
17831783
except HTTPError as e:
1784-
error_message = "Invalid user token."
1785-
# Check which token is the effective one and generate the error message accordingly
1786-
if effective_token == _get_token_from_google_colab():
1787-
error_message += " The token from Google Colab vault is invalid. Please update it from the UI."
1788-
elif effective_token == _get_token_from_environment():
1789-
error_message += (
1790-
" The token from HF_TOKEN environment variable is invalid. "
1791-
"Note that HF_TOKEN takes precedence over `hf auth login`."
1792-
)
1793-
elif effective_token == _get_token_from_file():
1794-
error_message += " The token stored is invalid. Please run `hf auth login` to update it."
1795-
raise HTTPError(error_message, request=e.request, response=e.response) from e
1784+
if e.response.status_code == 401:
1785+
error_message = "Invalid user token."
1786+
# Check which token is the effective one and generate the error message accordingly
1787+
if effective_token == _get_token_from_google_colab():
1788+
error_message += " The token from Google Colab vault is invalid. Please update it from the UI."
1789+
elif effective_token == _get_token_from_environment():
1790+
error_message += (
1791+
" The token from HF_TOKEN environment variable is invalid. "
1792+
"Note that HF_TOKEN takes precedence over `hf auth login`."
1793+
)
1794+
elif effective_token == _get_token_from_file():
1795+
error_message += " The token stored is invalid. Please run `hf auth login` to update it."
1796+
raise HTTPError(error_message, request=e.request, response=e.response) from e
1797+
raise
17961798
return r.json()
17971799

17981800
@_deprecate_method(

0 commit comments

Comments
 (0)