Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper handling custom made exceptions so can't handle them elsewhere

I am throwing custom exceptions inside my resolvers, but they are being caught and wrapped by Automapper so we cant handle them elsewhere in the program. I've included a simple example of the problem, the desired outcome is to catch an InterfaceError but it only catches an AutoMapperException with the InterfaceError as an inner exception.

In 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
            }
        }));
like image 392
user2248441 Avatar asked Mar 14 '14 15:03

user2248441


People also ask

How do I ignore properties in AutoMapper?

So, the AutoMapper Ignore() method is used when you want to completely ignore the property in the mapping. The ignored property could be in either the source or the destination object.

How does AutoMapper work internally?

How AutoMapper works? AutoMapper internally uses a great concept of programming called Reflection. Reflection in C# is used to retrieve metadata on types at runtime. With the help of Reflection, we can dynamically get a type of existing objects and invoke its methods or access its fields and properties.

What is AutoMapper good for?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.

When should I use AutoMapper?

Use AutoMapper to eliminate the need to write tedious boilerplate code when mapping objects in your application. AutoMapper is a popular object-to-object mapping library that can be used to map objects belonging to dissimilar types.


1 Answers

Automapper doesn't let an exception escape directly from the mapping process, but tries and wrap it in its own Exception as you noticed. Your best bet would be to wrap the call to Map inside a function that catches AutoMapperMappingException and unwrap it

public ToClass Map<FromClass, ToClass>(FromClass fc)
{
    try
    {
        return Mapper.Map<FromClass, ToClass>(fc);
    }
    catch(AutoMapperMappingException autoMapperException)
    {
        throw autoMapperException.InnerException; 
        // this will break your call stack
        // you may not know where the error is called and rather
        // want to clone the InnerException or throw a brand new Exception
    }
}
like image 175
samy Avatar answered Nov 15 '22 16:11

samy