Skip to content

Commit

Permalink
fix: Propagate exceptions from a message handler biesnecker#3
Browse files Browse the repository at this point in the history
  • Loading branch information
saaj committed Jun 8, 2015
1 parent f6832ec commit b406fed
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions cleveland/actor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from .message import StopMessage
import asyncio
import warnings

from .message import StopMessage


class HandlerNotFoundError(KeyError): pass

Expand Down Expand Up @@ -79,19 +82,21 @@ def register_handler(self, message_cls, func):
def _task(self):
message = yield from self._inbox.get()
try:

response = yield from self._handlers[type(message)](message)

# If the message is expecting a result, resolve it.
handler = self._handlers[type(message)]
is_query = isinstance(message, QueryMessage)
try:
message.result.set_result(response)
except AttributeError:
pass

except KeyError as e:
raise HandlerNotFoundError(type(message)) from e


response = yield from handler(message)
except Exception as ex:
if is_query:
message.result.set_exception(ex)
else:
warnings.warn('Unhandled exception from handler of {0}'.format(type(message)))
else:
if is_query:
message.result.set_result(response)
except KeyError as ex:
raise HandlerNotFoundError(type(message)) from ex

@asyncio.coroutine
def _stop(self):
yield from self._receive(StopMessage())
Expand Down

0 comments on commit b406fed

Please sign in to comment.