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

Refacto error parsing (HfHubHttpError) #2474

Merged
merged 5 commits into from
Aug 26, 2024
Merged

Refacto error parsing (HfHubHttpError) #2474

merged 5 commits into from
Aug 26, 2024

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Aug 21, 2024

(sorry for long PR, most of it is moving logic around)
Done as part of #2069.

This PR updates how HfHubHttpError is formatted. With #2069, all errors are now defined in src/huggingface_hub/errors.py with no logic it the module. Before this PR, the logic to format the error message from a server response was split between HfHubHttpError.__init__ and hf_raise_for_status. This should not have being split in the first place (harder to read/investigate)

What does this PR do?

  • move everything from HfHubHttpError.__init__ to hf_raise_for_status. This is the only way to instantiate a HfHubHttpError anyway
  • move hf_raise_for_status definition inside ./utils/_http.py where it belongs instead of ./utils/_errors.py
  • delete ./utils/_errors.py module to avoid confusion with ./errors.py where all exceptions are defined
  • (fix or feature?) when an error is returned by the server as a raw payload -not json formatted-, we must add the server message to the error message. This was not the case before. It is especially useful when TGI returns a 422 unprocessable entity with explanation as raw text
  • keep request_id / server_message as attributes. I'm pretty sure it's not used by anyone but at least it keeps backward compatibility.
  • adapt tests to pass (and remove occurrences of self.assertEquals...)

In the end, sorry for the long diff because 95% of it is simply moving some logic around.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Wauplin

This comment was marked as duplicate.

Copy link
Contributor

@osanseviero osanseviero left a comment

Choose a reason for hiding this comment

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

Overall LGTM! Will take a second pass

src/huggingface_hub/utils/__init__.py Show resolved Hide resolved
@@ -11,117 +11,117 @@
RepositoryNotFoundError,
RevisionNotFoundError,
)
from huggingface_hub.utils._errors import REPO_API_REGEX, hf_raise_for_status
from huggingface_hub.utils._http import REPO_API_REGEX, X_REQUEST_ID, _format, hf_raise_for_status
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the tests be moved to https://github.com/huggingface/huggingface_hub/blob/main/tests/test_utils_http.py for consistency with the rest of the codebase?

severo added a commit to huggingface/dataset-viewer that referenced this pull request Aug 21, 2024
severo added a commit to huggingface/dataset-viewer that referenced this pull request Aug 21, 2024
Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

Thanks for the refactor @Wauplin!

Comment on lines +25 to +26
assert context.exception.response.status_code == 404
assert "Request ID: 123" in str(context.exception)
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, why the change from self.assertEqual/self.assertIn to blank asserts with boolean statements? For consistency with new tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mostly for consistency yes. I'm only doing it when I'm adding tests next to existing ones (see related convo on slack -private-)

@Wauplin
Copy link
Contributor Author

Wauplin commented Aug 26, 2024

Thanks for the review @LysandreJik! I addressed @osanseviero 's comment and resolved the merge conflicts. Failing CI is unrelated so I'm merging now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants