I have a query handler decorator which logs any exceptions:
public class QueryHandlerLogDecorator<TQuery, TResult> : IQueryHandler<TQuery, TResult>
where TQuery : IQuery<TResult>
{
private readonly IQueryHandler<TQuery, TResult> _handler;
private readonly ILog _log;
public QueryHandlerLogDecorator(IQueryHandler<TQuery, TResult> handler)
{
_handler = handler;
_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
}
public TResult Handle(TQuery query)
{
try
{
var result = _handler.Handle(query);
_log.Info("Ok");
return result;
}
catch (Exception ex)
{
_log.Error(ex.Message, ex);
throw new Exception(ex.Message);
}
}
}
Whilst the exception handling isn't the worst thing in the world, what it means is that I lose the type of the exception being thrown.
For example, if I throw ApplicationException
lower down in the application, this is caught and rethrown as Exception
.
How can I rethrow the exception being caught as the original type?