Exception is thrown if the message handler in a IResponder throws a unhandled exception.
Наследование: System.Exception
Пример #1
0
        public virtual Task OnResponseHandlerExceptionAsync(IRawConsumer rawConsumer, IConsumerConfiguration cfg, BasicDeliverEventArgs args, Exception exception)
        {
            _logger.LogError($"An unhandled exception was thrown in the response handler for message '{args.BasicProperties.MessageId}'.", exception);
            var innerException = UnwrapInnerException(exception);
            var rawException = new MessageHandlerException(
                message: $"An unhandled exception was thrown when consuming a message\n  MessageId: {args.BasicProperties.MessageId}\n  Queue: '{cfg.Queue.FullQueueName}'\n  Exchange: '{cfg.Exchange.ExchangeName}'\nSee inner exception for more details.",
                inner: innerException
            );

            _logger.LogInformation($"Sending MessageHandlerException with CorrelationId '{args.BasicProperties.CorrelationId}'");
            rawConsumer.Model.BasicPublish(
                exchange: string.Empty,
                routingKey: args.BasicProperties?.ReplyTo ?? string.Empty,
                basicProperties: _propertiesProvider.GetProperties<MessageHandlerException>(p =>
                {
                    p.CorrelationId = args.BasicProperties?.CorrelationId ?? string.Empty;
                    p.Headers.Add(PropertyHeaders.ExceptionHeader, _messageExceptionName);
                }),
                body: _serializer.Serialize(rawException)
            );

            if (!cfg.NoAck)
            {
                _logger.LogDebug($"Nack'ing message with delivery tag '{args.DeliveryTag}'.");
                rawConsumer.Model.BasicNack(args.DeliveryTag, false, false);
            }
            return Task.FromResult(true);
        }
Пример #2
0
		public virtual Task OnResponseRecievedAsync(BasicDeliverEventArgs args, TaskCompletionSource<object> responseTcs)
		{
			var containsException = args?.BasicProperties?.Headers?.ContainsKey(PropertyHeaders.ExceptionHeader) ?? false;

			if (containsException)
			{
				_logger.LogInformation($"Message '{args.BasicProperties.MessageId}' withh CorrelationId '{args.BasicProperties.CorrelationId}' contains exception. Deserialize and re-throw.");
				var exceptionInfo = _serializer.Deserialize<ExceptionInformation>(args.Body);
				var exception = new MessageHandlerException(exceptionInfo.Message)
				{
					InnerExceptionType = exceptionInfo.ExceptionType,
					InnerStackTrace = exceptionInfo.StackTrace,
					InnerMessage = exceptionInfo.InnerMessage
				};
				responseTcs.TrySetException(exception);
			}

			return Task.FromResult(true);
		}