1
0
mirror of synced 2024-07-01 00:07:39 +00:00

Fixed #11: Improve stack traces for exceptions

Fixed #12: Stack overflow when an exception occurs in the middleware
This commit is contained in:
Mark van Renswoude 2017-02-14 22:45:59 +01:00
parent 52ad9a4fc6
commit 0a9bc09884

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.ExceptionServices;
using RabbitMQ.Client; using RabbitMQ.Client;
using Tapeti.Config; using Tapeti.Config;
using Tapeti.Helpers; using Tapeti.Helpers;
@ -32,6 +33,7 @@ namespace Tapeti.Connection
public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey,
IBasicProperties properties, byte[] body) IBasicProperties properties, byte[] body)
{ {
ExceptionDispatchInfo exception = null;
try try
{ {
var message = dependencyResolver.Resolve<IMessageSerializer>().Deserialize(body, properties); var message = dependencyResolver.Resolve<IMessageSerializer>().Deserialize(body, properties);
@ -81,19 +83,23 @@ namespace Tapeti.Connection
if (!validMessageType) if (!validMessageType)
throw new ArgumentException($"Unsupported message type: {message.GetType().FullName}"); throw new ArgumentException($"Unsupported message type: {message.GetType().FullName}");
worker.Respond(deliveryTag, ConsumeResponse.Ack);
} }
catch (Exception e) catch (Exception e)
{ {
worker.Respond(deliveryTag, exceptionStrategy.HandleException(context, UnwrapException(e))); exception = ExceptionDispatchInfo.Capture(UnwrapException(e));
worker.Respond(deliveryTag, exceptionStrategy.HandleException(context, exception.SourceException));
} }
} }
worker.Respond(deliveryTag, ConsumeResponse.Ack);
} }
catch (Exception e) catch (Exception e)
{ {
worker.Respond(deliveryTag, exceptionStrategy.HandleException(null, UnwrapException(e))); exception = ExceptionDispatchInfo.Capture(UnwrapException(e));
worker.Respond(deliveryTag, exceptionStrategy.HandleException(null, exception.SourceException));
} }
exception?.Throw();
} }
@ -103,11 +109,14 @@ namespace Tapeti.Connection
// code using Tasks we have to unwrap these ourselves to get the proper // code using Tasks we have to unwrap these ourselves to get the proper
// exception directly instead of "Errors occured". We might lose // exception directly instead of "Errors occured". We might lose
// some stack traces in the process though. // some stack traces in the process though.
var aggregateException = exception as AggregateException; while (true)
if (aggregateException != null && aggregateException.InnerExceptions.Count == 1) {
throw aggregateException.InnerExceptions[0]; var aggregateException = exception as AggregateException;
if (aggregateException == null || aggregateException.InnerExceptions.Count != 1)
return exception;
return UnwrapException(exception); exception = aggregateException.InnerExceptions[0];
}
} }