Skip to content

Conversation

frblondin
Copy link

While defining a new mapping between two classes, I was getting a mysterious Can't resolve this to Queryable Expression error message. The message was not meaningful enough so I had to debug AutoMapper to understand what the problem was. I simply had to tell AutoMapper which constructor had to be used...

This PR intends to generate a more explanatory error message.

public Expression NewExpression(Expression instanceParameter)
{
var parameters = CtorParams.Select(map =>
try
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the extra try/catch. Some exceptions are really bugs and need no user friendly messages.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right


namespace AutoMapper.QueryableExtensions
{
public class BuilderException : Exception
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a new exception type here. AutoMapperMappingException should do. Just pass it the types so it's easy to understand what's going on.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uses now AutoMapperMappingException

{
cfg.CreateMap<EntitySource, EntityDestination>();
});
var exception = Assert.Throws<BuilderException>(() => config.ExpressionBuilder.GetMapExpression<EntitySource, EntityDestination>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProjectTo is the API, so the test should call that. GetMapExpression is just an internal thing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProjectTo is now used by unit test.

result = matchingExpressionConverter?.GetExpressionResolutionResult(result, map)
?? throw new Exception("Can't resolve this to Queryable Expression");
result = matchingExpressionConverter?.GetExpressionResolutionResult(result, map)
?? throw new AutoMapperMappingException($"Unable to generate the instantiation expression for the constructor {Ctor}: no expression could be mapped for constructor parameter '{map.Parameter}'.", null, TypeMap.Types, null, null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last two nulls are not needed.

@jbogard jbogard merged commit 096e58a into LuckyPennySoftware:master Nov 23, 2017
@jbogard jbogard added this to the 6.2.2 milestone Dec 6, 2017
@lock
Copy link

lock bot commented May 5, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants