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
Conversation
src/AutoMapper/ConstructorMap.cs
Outdated
@@ -35,19 +35,26 @@ public ConstructorMap(ConstructorInfo ctor, TypeMap typeMap) | |||
|
|||
public Expression NewExpression(Expression instanceParameter) | |||
{ | |||
var parameters = CtorParams.Select(map => | |||
try |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>()); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
src/AutoMapper/ConstructorMap.cs
Outdated
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); |
There was a problem hiding this comment.
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.
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. |
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.