rebus-org / Rebus.Events

:bus: Convenient event configuration extensions for Rebus
https://mookid.dk/category/rebus
Other
17 stars 1 forks source link

Determine exception within AfterMessageHandled #1

Closed JonnyBooker closed 7 years ago

JonnyBooker commented 7 years ago

I am using the IHandlesMessages pattern to handle messages. I have forced an exception within the Handle logic and the AfterMessageHandled is now being hit. I was wondering if is possible to tell if now the message has handled that you can tell if the handling of a message threw an exception?

RebusConfigurer.Events(events =>
{
    events.BeforeMessageHandled += (bus, headers, message, context, args) =>
    {
        headers[HandledDateTimeHeaderName] = DateTime.UtcNow.ToString();
    };
    events.BeforeMessageSent += (bus, headers, message, context) =>
    {
        headers[MessageSentDateTimeHeaderName] = DateTime.UtcNow.ToString();
    };

    events.AfterMessageHandled += (bus, headers, message, context, args) =>
    {
        var messageQueueObject = message as IMessageQueueObject;
        var beforeHandledDateTime = DateTime.Parse(headers[HandledDateTimeHeaderName]);
        var difference = DateTime.UtcNow - beforeHandledDateTime;

        //TODO: Determine if message failed handling logic
        //...
    };
    events.AfterMessageSent += (bus, headers, message, context) =>
    {
        var messageQueueObject = message as IMessageQueueObject;
        var beforeHandledDateTime = DateTime.Parse(headers[MessageSentDateTimeHeaderName]);
        var difference = DateTime.UtcNow - beforeHandledDateTime;

        //...
    };
});
mookid8000 commented 7 years ago

While it's not immediately obvious, it IS actually possible to tell that an exception occurred while handling a message from within the AfterMessageHandled listener.

As you can see here the exception gets stashed in the IncomingStepContext when caught, allowing you to retrieve it from your listener by calling

var exceptionOrNull = context.Load<Exception>();

if (exceptionOrNull == null) return; //< nothing to do

// inspect exception here - possibly doing this if you want
// to ignore it:
args.IgnoreException = true;
JonnyBooker commented 7 years ago

Just what I need. Thank you very much!

vedaantees-archived commented 5 years ago

for events.AfterMessageHandled += (bus, headers, message, context, args) , I am getting null for IBus. I am using Autofac as DI. Any idea why?

mookid8000 commented 5 years ago

Thanks @abhay-naik for reporting this 😄 it was a genuine bug, which I have now fixed. The fix is available in Rebus.Events 4.0.1, which is on NuGet.org in a few minutes.

vedaantees-archived commented 5 years ago

You are so awesome....thanks so much.

On Sun, Sep 30, 2018, 8:13 PM Mogens Heller Grabe notifications@github.com wrote:

Thanks @abhay-naik https://github.com/abhay-naik for reporting this 😄 it was a genuine bug, which I have now fixed. The fix is available in Rebus.Events 4.0.1, which is on NuGet.org in a few minutes.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rebus-org/Rebus.Events/issues/1#issuecomment-425725740, or mute the thread https://github.com/notifications/unsubscribe-auth/ABEwngBY_fUFVbR0wDSvqiu8ZiLKL2JPks5ugNiqgaJpZM4MECE8 .