[TASK] Rethrow unhandled exceptions properly.

This commit is contained in:
Jan Philipp Timme 2015-10-24 18:40:52 +02:00
parent 5c53fd79df
commit e31bd1700b
1 changed files with 7 additions and 7 deletions

14
bot.py
View File

@ -61,17 +61,17 @@ class ConnectionManager(object):
self._loop.set_exception_handler(self._handle_async_exception) self._loop.set_exception_handler(self._handle_async_exception)
def add_endpoint(self, endpoint): def add_endpoint(self, endpoint):
logger.debug("Endpoint added: {}:{}".format(endpoint[0],endpoint[1])) logger.debug("Endpoint added: {}:{}".format(*endpoint))
self._endpoints.append(endpoint) self._endpoints.append(endpoint)
self._create_connection(endpoint) self._create_connection(endpoint)
def _create_connection(self, endpoint): def _create_connection(self, endpoint):
protocol = ManagedProtocol(self._loop, self, endpoint) protocol = ManagedProtocol(self._loop, self, endpoint)
coroutine = self._loop.create_connection(lambda: protocol, endpoint[0], endpoint[1]) coroutine = self._loop.create_connection(lambda: protocol, *endpoint)
asyncio.async(coroutine) asyncio.async(coroutine)
def remove_endpoint(self, endpoint): def remove_endpoint(self, endpoint):
logger.debug("Endpoint removed: {}:{}".format(endpoint[0],endpoint[1])) logger.debug("Endpoint removed: {}:{}".format(*endpoint))
self._endpoints.remove(endpoint) self._endpoints.remove(endpoint)
if endpoint in self._active_connections: if endpoint in self._active_connections:
self._active_connections[endpoint].close() self._active_connections[endpoint].close()
@ -94,19 +94,19 @@ class ConnectionManager(object):
port = call_args.locals['port'] port = call_args.locals['port']
logger.error("Bad endpoint: {}:{}".format(host, port)) logger.error("Bad endpoint: {}:{}".format(host, port))
self.remove_endpoint((host, port)) self.remove_endpoint((host, port))
return True else:
return False loop.call_exception_handler(context)
if __name__ == '__main__': if __name__ == '__main__':
freenode = ("irc.freenode.net", 6667) freenode = ("irc.freenode.net", 6667)
euirc = ("irc.esduirc.net", 6267) euirc = ("irc.euisadasdrc.net", 6667)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
connection_manager = ConnectionManager(loop) connection_manager = ConnectionManager(loop)
connection_manager.add_endpoint(euirc) connection_manager.add_endpoint(euirc)
#connection_manager.add_endpoint(freenode) connection_manager.add_endpoint(freenode)
try: try:
loop.run_forever() loop.run_forever()