I throw custom exceptions inside my resolvers, but they are caught and wrapped by Automapper, so we cannot handle them elsewhere in the program. I included a simple example of the problem, the desired result is to catch an InterfaceError, but it only catches the AutoMapperException with the Errror interface as an internal exception.
In the class:
public Order MapOrder(InterfaceOrder iOrder)
{
try
{
Order mappedOrder = Mapper.Map<InterfaceOrder, Order>(iOrder);
}
catch (InterfaceException ex)
{
Log("Interface error");
}
catch (Exception ex)
{
Log("error");
}
return mappedOrder;
}
Mapping:
Mapper.CreateMap<InterfaceOrder, Order>()
.ForMember(c => c.Name, op => op.ResolveUsing(data =>
{
if (Name.Length > 50)
{
throw new InterfaceException("Error!", ex);
}
else
{
return c.Name
}
}));
source
share