diff --git a/src/AutoMapper/Execution/TypeMapPlanBuilder.cs b/src/AutoMapper/Execution/TypeMapPlanBuilder.cs index 77bde941ca..5fe693bf8e 100644 --- a/src/AutoMapper/Execution/TypeMapPlanBuilder.cs +++ b/src/AutoMapper/Execution/TypeMapPlanBuilder.cs @@ -254,7 +254,14 @@ private void AddPathMaps(List actions) continue; } - actions.Add(TryPathMap(pathMap)); + try + { + actions.Add(TryPathMap(pathMap)); + } + catch (Exception e) when (e is not AutoMapperConfigurationException) + { + throw new AutoMapperMappingException("Error building path mapping strategy.", e, pathMap); + } } } @@ -287,14 +294,21 @@ private void AddPropertyMaps(List actions) continue; } - var property = TryMemberMap(propertyMap, - CreatePropertyMapFunc(propertyMap, _destination, propertyMap.DestinationMember)); - if (_typeMap.ConstructorParameterMatches(propertyMap.DestinationName)) + try { - property = _initialDestination.IfNullElse(_configuration.Default(property.Type), property); + var property = TryMemberMap(propertyMap, + CreatePropertyMapFunc(propertyMap, _destination, propertyMap.DestinationMember)); + if (_typeMap.ConstructorParameterMatches(propertyMap.DestinationName)) + { + property = _initialDestination.IfNullElse(_configuration.Default(property.Type), property); + } + + actions.Add(property); + } + catch (Exception e) when (e is not AutoMapperConfigurationException) + { + throw new AutoMapperMappingException("Error building member mapping strategy.", e, propertyMap); } - - actions.Add(property); } } @@ -366,9 +380,16 @@ private Expression ConstructorMapping(ConstructorMap constructorMap) List body = []; foreach (var parameter in constructorMap.CtorParams) { - var variable = Variable(parameter.DestinationType, parameter.DestinationName); - variables.Add(variable); - body.Add(Assign(variable, CreateConstructorParameterExpression(parameter))); + try + { + var variable = Variable(parameter.DestinationType, parameter.DestinationName); + variables.Add(variable); + body.Add(Assign(variable, CreateConstructorParameterExpression(parameter))); + } + catch (Exception e) when (e is not AutoMapperConfigurationException) + { + throw new AutoMapperMappingException("Error building constructor parameter mapping strategy.", e, parameter); + } } body.Add(CheckReferencesCache(New(constructorMap.Ctor, variables))); diff --git a/src/AutoMapper/QueryableExtensions/ProjectionBuilder.cs b/src/AutoMapper/QueryableExtensions/ProjectionBuilder.cs index 835b69e1a2..7f73c791c9 100644 --- a/src/AutoMapper/QueryableExtensions/ProjectionBuilder.cs +++ b/src/AutoMapper/QueryableExtensions/ProjectionBuilder.cs @@ -110,10 +110,18 @@ void ProjectProperties() { continue; } - var propertyProjection = TryProjectMember(propertyMap); - if(propertyProjection != null) + + try { - propertiesProjections.Add(Bind(propertyMap.DestinationMember, propertyProjection)); + var propertyProjection = TryProjectMember(propertyMap); + if(propertyProjection != null) + { + propertiesProjections.Add(Bind(propertyMap.DestinationMember, propertyProjection)); + } + } + catch (Exception e) when (e is not AutoMapperConfigurationException) + { + throw new AutoMapperMappingException("Error building queryable mapping strategy.", e, propertyMap); } } } @@ -209,7 +217,17 @@ IProjectionMapper GetProjectionMapper() { { CustomCtorExpression: LambdaExpression ctorExpression } => (NewExpression)ctorExpression.ReplaceParameters(instanceParameter), { ConstructorMap: { CanResolve: true } constructorMap } => - New(constructorMap.Ctor, constructorMap.CtorParams.Select(map => TryProjectMember(map, map.DefaultValue(null)) ?? Default(map.DestinationType))), + New(constructorMap.Ctor, constructorMap.CtorParams.Select(map => + { + try + { + return TryProjectMember(map, map.DefaultValue(null)) ?? Default(map.DestinationType); + } + catch (Exception e) when (e is not AutoMapperConfigurationException) + { + throw new AutoMapperMappingException("Error building constructor projection strategy.", e, map); + } + })), _ => New(typeMap.DestinationType) }; } diff --git a/src/AutoMapper/TypeMap.cs b/src/AutoMapper/TypeMap.cs index 6e80954be0..07ef03c48b 100644 --- a/src/AutoMapper/TypeMap.cs +++ b/src/AutoMapper/TypeMap.cs @@ -204,8 +204,15 @@ public void Seal(IGlobalConfiguration configuration) return; } _sealed = true; - _details?.Seal(configuration, this); - MapExpression = Projection ? EmptyLambda : CreateMapperLambda(configuration); + try + { + _details?.Seal(configuration, this); + MapExpression = Projection ? EmptyLambda : CreateMapperLambda(configuration); + } + catch (Exception e) when (e is not AutoMapperConfigurationException) + { + throw new AutoMapperMappingException("Error creating mapping strategy.", e, this); + } SourceTypeDetails = null; DestinationTypeDetails = null; } diff --git a/src/UnitTests/ForPath.cs b/src/UnitTests/ForPath.cs index b74bbe3be7..0c944747e6 100644 --- a/src/UnitTests/ForPath.cs +++ b/src/UnitTests/ForPath.cs @@ -250,11 +250,11 @@ public class SourceModel [Fact] public void Should_throw_exception() { - Assert.Throws(() => + Assert.Throws(() => { var cfg = new MapperConfiguration(config => { - Assert.Throws(() => + Assert.Throws(() => { config.CreateMap() .ForPath(sourceModel => sourceModel.Name, opts => opts.MapFrom(null));