private string CreateMessagingErrorMessage(string description, object payload)
 {
     return(description + "\n"
            + "Endpoint handler details:\n"
            + "Method [" + HandlerAdapter.GetMethodAsString(payload) + "]\n"
            + "Bean [" + HandlerAdapter.Instance + "]");
 }
        private void ReturnOrThrow(Message amqpMessage, IModel channel, IMessage message, Exception exceptionToRetrun, Exception exceptionToThrow)
        {
            if (!ReturnExceptions)
            {
                throw exceptionToThrow;
            }

            try
            {
                HandleResult(
                    new InvocationResult(
                        exceptionToRetrun,
                        null,
                        HandlerAdapter.GetReturnTypeFor(message.Payload),
                        HandlerAdapter.Instance,
                        HandlerAdapter.GetMethodFor(message.Payload)),
                    amqpMessage,
                    channel,
                    message);
            }
            catch (ReplyFailureException rfe)
            {
                if (typeof(void).Equals(HandlerAdapter.GetReturnTypeFor(message.Payload)))
                {
                    throw exceptionToThrow;
                }
                else
                {
                    throw;
                }
            }
        }
        protected void InvokeHandlerAndProcessResult(Message amqpMessage, IModel channel, IMessage message)
        {
            _logger?.LogDebug("Processing [" + message + "]");
            InvocationResult result = null;

            try
            {
                if (MessagingMessageConverter.Method == null)
                {
                    amqpMessage.MessageProperties.TargetMethod = HandlerAdapter.GetMethodFor(message.Payload);
                }

                result = InvokeHandler(amqpMessage, channel, message);
                if (result.ReturnValue != null)
                {
                    HandleResult(result, amqpMessage, channel, message);
                }
                else
                {
                    _logger?.LogTrace("No result object given - no result to handle");
                }
            }
            catch (ListenerExecutionFailedException e)
            {
                if (ErrorHandler != null)
                {
                    try
                    {
                        var messageWithChannel = Messaging.Support.MessageBuilder.FromMessage(message).SetHeader(AmqpHeaders.CHANNEL, channel).Build();
                        var errorResult        = ErrorHandler.HandleError(amqpMessage, messageWithChannel, e);
                        if (errorResult != null)
                        {
                            HandleResult(HandlerAdapter.GetInvocationResultFor(errorResult, message.Payload), amqpMessage, channel, message);
                        }
                        else
                        {
                            _logger?.LogTrace("Error handler returned no result");
                        }
                    }
                    catch (Exception ex)
                    {
                        ReturnOrThrow(amqpMessage, channel, message, ex, ex);
                    }
                }
                else
                {
                    ReturnOrThrow(amqpMessage, channel, message, e.InnerException, e);
                }
            }
        }
 private InvocationResult InvokeHandler(Message amqpMessage, IModel channel, IMessage message)
 {
     try
     {
         return(HandlerAdapter.Invoke(message, amqpMessage, channel));
     }
     catch (MessagingException ex)
     {
         throw new ListenerExecutionFailedException(
                   CreateMessagingErrorMessage("Listener method could not be invoked with the incoming message", message.Payload), ex, amqpMessage);
     }
     catch (Exception ex)
     {
         throw new ListenerExecutionFailedException("Listener method '" + HandlerAdapter.GetMethodAsString(message.Payload) + "' threw exception", ex, amqpMessage);
     }
 }