Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message when the creation of the instantiation expression fails #2421

Merged
merged 2 commits into from Nov 23, 2017

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.

@@ -35,19 +35,26 @@ public ConstructorMap(ConstructorInfo ctor, TypeMap typeMap)

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

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
Member

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
Member

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
Member

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 AutoMapper: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.

None yet

3 participants