Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there any way to disable AutoMapper's exception wrapping?
    text
    copied!<p>I have a repository that will throw an <code>EntityNotFoundException</code> when its <code>GetSingle&lt;T&gt;(int id)</code> method cannot find the requested entity in the database. When I use this with AutoMapper and an exception occurs, I get something like this instead:</p> <blockquote> <p>AutoMapperMappingException: Trying to map CategoryDTO to Category... ---></p> <p>AutoMapperMappingException: Trying to map System.Int32 to CategoryType... ---></p> <p>AutoMapper.MappingException: Trying to map System.Int32 to CategoryType... ---></p> <p>EntityNotFoundException: entity of type CategoryType with ID 5 was not found in the database</p> </blockquote> <p>My custom exception is 4 levels down. This makes it hard to use try-catch blocks because now I have to do something like this:</p> <pre><code>try { // do the mapping } catch (AutoMapperMappingException e) { // get the inner-most exception while (e.InnerException != null) { e = e.InnerException; } // check to see if it's an EntityNotFoundException if (e.GetType() == typeof (EntityNotFoundException)) { var notFound = e as EntityNotFoundException; // do something specific here, like inform the user } else { // do something more generic } </code></pre> <p>What I'd like to be able to do is just this:</p> <pre><code>try { // do the mapping } catch (EntityNotFoundException e) { // do something specific here, like inform the user } catch (Exception e) { // do something more generic } </code></pre> <p>Is there any way of disabling AutoMapper's exception wrapping behavior so that I get the straight-up exceptions that are being thrown?</p> <h2>Answer</h2> <p>I ended up creating a thin wrapper around <code>AutoMapper</code> that will catch the <code>AutoMapperMappingException</code>, find the inner-most exception, and throw that:</p> <pre><code>public class AutoMapperWrapper { public TB Map&lt;TA, TB&gt;(TA source, TB destination) { // try to do the mapping try { return Mapper.Map(source, destination); } // catch AutoMapper's exception catch (Exception e) { // find the first InnerException that's not wrapped while (e is AutoMapperMappingException) { e = e.InnerException; } // if the inner exception is null, throw the original exception if (e == null) { throw; } // otherwise, throw the inner exception else { throw e; } } } } </code></pre> <p>The downside of this method though is that sometimes the entire exception tree is useful to look at to see which property or entity mapping AutoMapper failed on, but this code will only give you the inner-most exception, which is sometimes not very helpful by itself, like <code>InvalidCastException</code>: "could not convert string to int", but won't tell you which property it was.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload