Skip to content

Commit

Permalink
feat: Add exception argument to errback function
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jan 19, 2022
1 parent 7f2bc69 commit 69355c2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

0.0.8 (2022-01-19)
------------------

Added
~~~~~

- :meth:`yapw.decorators.decorate` passes the exception instance to the ``errback`` function via its ``exception`` argument.

0.0.7 (2022-01-18)
------------------

Expand Down
2 changes: 1 addition & 1 deletion tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def closes(*args):


def finalback(decode, callback, state, channel, method, properties, body):
def errback():
def errback(exception):
logger.warning("errback")

def finalback():
Expand Down
14 changes: 7 additions & 7 deletions yapw/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
def myfunction(decode, callback, state, channel, method, properties, body):
def errback():
def errback(exception):
# do something, like halting the process or nack'ing the message
decorate(decode, callback, state, channel, method, properties, body, errback)
Expand Down Expand Up @@ -65,7 +65,7 @@ def decorate(
method: pika.spec.Basic.Deliver,
properties: pika.BasicProperties,
body: bytes,
errback: Callable[[], None],
errback: Callable[[Exception], None],
finalback: Optional[Callable[[], None]] = None,
) -> None:
"""
Expand All @@ -87,8 +87,8 @@ def decorate(
message = decode(body, properties.content_type)
try:
callback(state, channel, method, properties, message)
except Exception:
errback()
except Exception as exception:
errback(exception)
finally:
if finalback:
finalback()
Expand All @@ -111,7 +111,7 @@ def halt(
If the callback raises an exception, send the SIGUSR1 signal to the main thread, without acknowledgment.
"""

def errback() -> None:
def errback(exception) -> None:
logger.exception("Unhandled exception when consuming %r, sending SIGUSR1", body)
os.kill(os.getpid(), signal.SIGUSR1)

Expand All @@ -131,7 +131,7 @@ def discard(
If the callback raises an exception, nack the message without requeueing.
"""

def errback() -> None:
def errback(exception) -> None:
logger.exception("Unhandled exception when consuming %r, discarding message", body)
nack(state, channel, method.delivery_tag, requeue=False)

Expand All @@ -151,7 +151,7 @@ def requeue(
If the callback raises an exception, nack the message, and requeue the message unless it was redelivered.
"""

def errback() -> None:
def errback(exception) -> None:
requeue = not method.redelivered
logger.exception("Unhandled exception when consuming %r (requeue=%r)", body, requeue)
nack(state, channel, method.delivery_tag, requeue=requeue)
Expand Down

0 comments on commit 69355c2

Please sign in to comment.