-
Notifications
You must be signed in to change notification settings - Fork 147
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
Including http_error as attribute in exception #445
Comments
We raise from requests, you should be able to parse it https://github.com/devopshq/artifactory/blob/master/dohq_artifactory/exception.py#L21 |
You mean by accessing import artifactory
import requests
def should_retry(exception) -> bool:
recoverable_errors = {429}
dohq_exception = isinstance(exception, artifactory.ArtifactoryException)
raised_from = dohq_exception.__cause__
if dohq_exception and raised_from is not None:
requests_exception = isinstance(raised_from, requests.exceptions.HTTPError)
if requests_exception and requests_exception.response is not None:
if requests_exception.response.status_code in {recoverable_errors}:
return True
return False Or is there a way to access |
it feels somehow wrong first of all, you can directly catch HTTP exceptions if you know that you expect 429. No need to catch artifactory exception. |
We're already using Tenacity. The snippet I posted is a variation of what we run in our codebase. I wanted to abstract that implementation detail. Correct me if I'm wrong, but in general your code is comparable to: # requests.py
class RequestsException(Exception):
message = "bar" # Not a direct dependency of any user of `dohq-artifactory`
...
# dohq-artifactory.py
class ArtifactoryException(Exception):
pass # Custom exception
def raise_exception():
"""Mimics code in dohq-artifactory that would raise exception."""
try:
raise RequestsException
except RequestsException as exc:
raise ArtifactoryException("Copied message") from exc # Not hiding the original exception is a good idea IMO Which means that your users have the following possibilities:
What I'm proposing in this issue is that the public API |
hmm, At the same time, where to draw the line of what should be attached to the simplest would be to attach the original response, but that will not solve the issue, since you anyway will depend on the implementation detail then @allburov thoughts on this ? |
I don't know the complexity of your project, but perhaps in this case it's worth having a Base exception and different exceptions types inheriting? class ArtifactoryException(Exception):
"""Base exception for all custom exceptions in dohq-artifactory."""
pass
class ArtifactoryHTTPError(ArtifactoryException): # PEP-8 recommends ending exception names with 'Error'
def __init__(self):
# ... perhaps inherit from requests.exceptions.HTTPError?
class ArtifactoryConfigError(ArtifactoryException):
"""Probably worth adding docstrings.""" I'm not a huge fan of custom exceptions, but I think that your dependency to Iff you only use From https://github.com/devopshq/artifactory?tab=readme-ov-file#exception-handling:
|
I agree on the point that we shouldn't even show the underlying library (requests) we use to make http requests and we should allow to switch that library by providing some |
what do you think of my proposal with the base exception? |
It looks to me like the current implementation of
ArtifactoryException
currently only includes the http error code as part of the string message (e.g.dohq_artifactory.exception.ArtifactoryException: 502 Server Error: Bad Gateway for url: https://myserver.com/artifactory/api/storage/my-repo/path/4242/directory
Unfortunately our infrastructure is sometimes unstable so we need to retry some operations in case of some specific errors (e.g. 429).
We prefer not to parse the exception message string, as there are some numbers (IDs) in the URL and we would create a dependency to the format of your exception message.
Instead, we think that including an optional (i.e. None on initialization)
http_error_code
would greatly help us. That is anyway what theHTTPError
ofrequests
includes: https://github.com/psf/requests/blob/main/src/requests/exceptions.py#L22The text was updated successfully, but these errors were encountered: