Skip to content

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

Merged
jbogard merged 2 commits into
LuckyPennySoftware:masterfrom
frblondin:master
Nov 23, 2017
Merged

Better error message when the creation of the instantiation expression fails#2421
jbogard merged 2 commits into
LuckyPennySoftware:masterfrom
frblondin:master

Conversation

@frblondin

Copy link
Copy Markdown

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.

Comment threadsrc/AutoMapper/ConstructorMap.cs Outdated
public Expression NewExpression(Expression instanceParameter)
{
var parameters = CtorParams.Select(map =>
try

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment threadsrc/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);

Copy link
Copy Markdown
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:masterNov 23, 2017
@jbogardjbogard added this to the 6.2.2 milestone Dec 6, 2017
@lock

lockBot commented May 5, 2019

Copy link
Copy Markdown

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.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@frblondin@lbargaoanu@jbogard