Exception Handling in Pornon Tornado

I am trying to handle the exception that occurred in AsyncClient.fetch as follows:

 from tornado.httpclient import AsyncHTTPClient from tornado.httpclient import HTTPRequest from tornado.stack_context import ExceptionStackContext from tornado import ioloop def handle_exc(*args): print('Exception occured') return True def handle_request(response): print('Handle request') http_client = AsyncHTTPClient() with ExceptionStackContext(handle_exc): http_client.fetch('http://some123site.com', handle_request) ioloop.IOLoop.instance().start() 

and see the following output:

 WARNING:root:uncaught exception Traceback (most recent call last): File "/home/crchemist/python-3.2/lib/python3.2/site-packages/tornado-2.0-py3.2.egg/tornado/simple_httpclient.py", line 259, in cleanup yield File "/home/crchemist/python-3.2/lib/python3.2/site-packages/tornado-2.0-py3.2.egg/tornado/simple_httpclient.py", line 162, in __init__ 0, 0) socket.gaierror: [Errno -5] No address associated with hostname Handle request 

What am I doing wrong?

+4
source share
2 answers

According to the Tornado documentation :

If an error occurs during fetching, the HTTPResponse set for the callback has an error attribute other than None, which contains an exception that was detected during the request. You can call response.rethrow() to throw an exception (if any) in the callback.

 from tornado.httpclient import AsyncHTTPClient from tornado.httpclient import HTTPRequest from tornado.stack_context import ExceptionStackContext from tornado import ioloop import traceback def handle_exc(*args): print('Exception occured') return True def handle_request(response): if response.error is not None: with ExceptionStackContext(handle_exc): response.rethrow() else: print('Handle request') http_client = AsyncHTTPClient() http_client.fetch('http://some123site.com', handle_request) http_client.fetch('http://google.com', handle_request) ioloop.IOLoop.instance().start() 

The message you see on the console is just a warning (sent via logging.warning ). This is harmless, but if it really bothers you, see the logging module for filtering it.

+9
source

I don't know Tornado at all, but I looked and you just can't catch exceptions that way. An exception is thrown in the _HTTPConnection () constructor, and most of the code in this constructor is already wrapped in another stack context:

  with stack_context.StackContext(self.cleanup): parsed = urlparse.urlsplit(_unicode(self.request.url)) [...] 

So, basically, when an exception is thrown (gaierror in your example), it is already caught and processed through self.cleanup, which in turn generates a 599 AFAICT response:

 @contextlib.contextmanager def cleanup(self): try: yield except Exception, e: logging.warning("uncaught exception", exc_info=True) self._run_callback(HTTPResponse(self.request, 599, error=e, request_time=time.time() - self.start_time, )) 

Not sure if this will answer your question.

+3
source

All Articles