-
-
Notifications
You must be signed in to change notification settings - Fork 313
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
When ServiceUnavailable, log error instead of exception (traceback) #764
Conversation
41d4a82
to
36ef828
Compare
subliminal/core.py
Outdated
elif r.status_code == 503: | ||
raise ServiceUnavailable | ||
else: | ||
r.raise_for_status() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this is not ideal to have unavailability errors go straight up as unhandled exceptions.
r.status_code == 503
already raises HTTPError with response attribute on which you can check the status code
try:
r.raise_for_status()
except Exception as e:
# e.reponse.status_code == 503
TooManyRequests
is quite provider specific, I'm not sure this is relevant to put this in a global function.
Sometimes raise_for_status
isn't used because one expects a redirect or something like that.
I suggest a less invasive approach: catch HTTPError with 503 status code pretty much as ServiceUnavailable and leave the TooManyRequests handling on a provider basis.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Diaoul thanks for reviewing.
I added TooManyRequests only because of Addi7ed. I will add a custom to the provider then
Just a fyi, I did all this based on OpenSubtitles:
https://github.com/Diaoul/subliminal/blob/develop/subliminal/providers/opensubtitles.py#L268-L294
should we change that method too as it checks 503 and others status codes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenSubtitles does that because there is no raise_for_status
available. I agree that ServiceUnavailable shouldn't be related to OpenSubtitles though but maybe a broader exception class within whole subliminal for providers that don't use requests
and the HTTPError exception that is.
subliminal/core.py
Outdated
if hasattr(e, 'response') and getattr(e.response, 'status_code', None) == 503: | ||
logger.error('Provider %r terminated unexpectedly', name) | ||
else: | ||
logger.exception('Provider %r terminated unexpectedly', name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Diaoul like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to catch HTTPError from requests no?
subliminal/core.py
Outdated
if e.response and e.response.status_code == 503: | ||
logger.error('Provider %r unavailable, improperly terminated', name) | ||
else: | ||
logger.exception('Provider %r http error, terminated unexpectedly', name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Diaoul all good?
@Diaoul can we make TooManyRequests from Addi7ed a "expected error" and also log as unavailable (or else) instead of log exception? It raises that error a lot in my tests with this PR
ping @pannal as per you comment in 304, maybe we can work this out here in this PR. what do you think? |
Sure. I think we definitely need a more fine grained provider status handling than what's currently implemented. Is 304 on addic7ed really their "too many requests" response? @fernandog you can join us on slack btw: https://szslack.fragstore.net |
this are my findings using a modified pytest Provider return a 304 and content = '' so that's why we have the indexError can we assume site is not available?
|
6df3f40
to
9b2d2c4
Compare
94802c2
to
c483df4
Compare
9cd2368
to
69bcb87
Compare
1 similar comment
Travis failing only for Addi7ed using lxml parser (known issue) |
503 status is not an unexpected error. Quite common btw Also OpenSubtitle raises a xmlrpclib.ProtocolError when unavailable: <ProtocolError for api.opensubtitles.org/xml-rpc: 503 Backend fetch failed> Full traceback: Unexpected error in provider 'opensubtitles' Traceback (most recent call last): File "/home/osmc/Medusa/lib/subliminal/core.py", line 118, in list_subtitles_provider return self[provider].list_subtitles(video, provider_languages) File "/home/osmc/Medusa/lib/subliminal/core.py", line 68, in __getitem__ provider.initialize() File "/home/osmc/Medusa/lib/subliminal/providers/opensubtitles.py", line 142, in initialize 'subliminal v%s' % __short_version__)) File "/usr/lib/python2.7/xmlrpclib.py", line 1233, in __call__ return self.__send(self.__name, args) File "/usr/lib/python2.7/xmlrpclib.py", line 1591, in __request verbose=self.__verbose File "/usr/lib/python2.7/xmlrpclib.py", line 1273, in request return self.single_request(host, handler, request_body, verbose) File "/usr/lib/python2.7/xmlrpclib.py", line 1321, in single_request response.msg, ProtocolError: <ProtocolError for api.opensubtitles.org/xml-rpc: 503 Backend fetch failed>
Provider wrongful return a status of 304 Not Modified with an empty content raise_for_status won't raise exception for that status code
69bcb87
to
7682984
Compare
1 similar comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like having the raise_for_status as a function as it only works for providers like requests and most importantly it makes adding new providers more difficult as you cannot just r.raise.. but have to use the utils.raise_for_status. I think the provider code should be as simple as possible, we can catch the requests Exception and check the status_code from within the exception handler instead. This would keep the initial intentions of this PR and reduce the footprint. What do you think?
@Diaoul imho 503 status is not an unexpected error (exception). It's quite common btw. This PR avoids showing a traceback to the user and its show an error log message instead
Provider {provider} unavailable, discarding it
Also OpenSubtitle raises a xmlrpclib.ProtocolError when unavailable
Full traceback is in the commit message.
What do you think?