Skip to content

Commit 2697568

Browse files
committed
Simplifying exception information creation
1 parent bf7cbf7 commit 2697568

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

AgileMapper.UnitTests/Configuration/WhenConfiguringObjectCreation.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,23 @@ public void ShouldUseConfiguredFactoriesForBaseAndDerivedTypes()
308308
[Fact]
309309
public void ShouldHandleAnObjectMappingDataCreationException()
310310
{
311-
using (var mapper = Mapper.CreateNew())
311+
var thrownException = Should.Throw<MappingException>(() =>
312312
{
313-
mapper.After.MappingEnds.Call(Console.WriteLine);
313+
using (var mapper = Mapper.CreateNew())
314+
{
315+
mapper.After.MappingEnds.Call(Console.WriteLine);
314316

315-
var source = new PublicField<Product> { Value = new Product() };
317+
var source = new PublicField<Product> { Value = new Product() };
316318

317-
var thrownException = Should.Throw<MappingException>(() => mapper.Map(source).ToANew<ExceptionThrower<Product>>());
319+
mapper.Map(source).ToANew<ExceptionThrower<Product>>();
318320

319-
thrownException.InnerException.ShouldNotBeNull();
320-
// ReSharper disable once PossibleNullReferenceException
321-
thrownException.InnerException.Message.ShouldBe(MappingException.NoMappingData);
322-
}
321+
}
322+
});
323+
324+
thrownException.InnerException.ShouldNotBeNull();
325+
326+
// ReSharper disable once PossibleNullReferenceException
327+
thrownException.InnerException.Message.ShouldContain("An exception occurred mapping");
323328
}
324329

325330
[Fact]

AgileMapper.UnitTests/WhenViewingMappingPlans.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ public void ShouldSupportAnonymousSourceTypesFromTheInstanceApi()
5555
.OnTo<Customer>();
5656

5757
plan.ShouldContain("Map AnonymousType<string, string> -> Customer");
58-
plan.ShouldContain(".Target.Name = sourceF__AnonymousType");
59-
plan.ShouldContain("_String_String.Name;");
60-
plan.ShouldContain("address.Line1 = sourceF__AnonymousType");
61-
plan.ShouldContain("_String_String.AddressLine1;");
58+
plan.ShouldContain(".Target.Name = fatssToCData.Source.Name");
59+
plan.ShouldContain("address.Line1 = fatssToCData.Source.AddressLine1");
6260
}
6361
}
6462

AgileMapper/MappingException.cs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
namespace AgileObjects.AgileMapper
22
{
33
using System;
4+
using System.Linq.Expressions;
45
using System.Reflection;
56
#if SERIALIZATION_SUPPORTED
67
using System.Runtime.Serialization;
78
#endif
9+
using Extensions.Internal;
810
using Members;
911
using NetStandardPolyfills;
10-
using ObjectPopulation;
1112

1213
/// <summary>
1314
/// Represents an error that occurred during a mapping.
@@ -19,11 +20,9 @@
1920
#endregion
2021
public class MappingException : Exception
2122
{
22-
internal static readonly MethodInfo FactoryMethod =
23+
private static readonly MethodInfo _factoryMethod =
2324
typeof(MappingException).GetPublicStaticMethod("For");
2425

25-
internal const string NoMappingData = "An exception occurred creating a mapping data instance";
26-
2726
#region Serialization Support
2827
#if SERIALIZATION_SUPPORTED
2928
/// <summary>
@@ -40,41 +39,44 @@ protected MappingException(SerializationInfo info, StreamingContext context)
4039
#endif
4140
#endregion
4241

43-
private MappingException(IMemberMapperData mapperData, Exception innerException)
44-
: base(GetMessage(mapperData), innerException)
42+
private MappingException(string message, Exception innerException)
43+
: base(message, innerException)
4544
{
4645
}
4746

4847
/// <summary>
4948
/// Creates a new instance of the MappingException class.
5049
/// </summary>
51-
/// <typeparam name="TSource">The source type being mapped when the exception occurred.</typeparam>
52-
/// <typeparam name="TTarget">The target type being mapped when the exception occurred.</typeparam>
53-
/// <param name="mappingData">
54-
/// The <see cref="IObjectMappingData{TSource, TTarget}"/> containing the mapping data of the
55-
/// current mapping context.
56-
/// </param>
50+
/// <param name="ruleSetName">The name of the mapping rule set being executed when the exception occurred.</param>
51+
/// <param name="sourcePath">The path of the source object being mapped when the exception occurred.</param>
52+
/// <param name="targetPath">The path of the target object being mapped when the exception occurred.</param>
5753
/// <param name="innerException">The exception which caused the creation of the MappingException.</param>
5854
/// <returns>A new MappingException instance.</returns>
59-
public static MappingException For<TSource, TTarget>(
60-
IObjectMappingData<TSource, TTarget> mappingData,
55+
public static MappingException For(
56+
string ruleSetName,
57+
string sourcePath,
58+
string targetPath,
6159
Exception innerException)
6260
{
63-
return new MappingException(((IObjectMappingData)mappingData)?.MapperData, innerException);
61+
return new MappingException(
62+
$"An exception occurred mapping {sourcePath} -> {targetPath} with rule set {ruleSetName}.",
63+
innerException);
6464
}
6565

66-
private static string GetMessage(IMemberMapperData mapperData)
66+
internal static Expression GetFactoryMethodCall(IMemberMapperData mapperData, Expression exceptionVariable)
6767
{
68-
if (mapperData == null)
69-
{
70-
return NoMappingData;
71-
}
72-
7368
var rootData = mapperData.GetRootMapperData();
7469
var sourcePath = mapperData.SourceMember.GetFriendlySourcePath(rootData);
7570
var targetPath = mapperData.TargetMember.GetFriendlyTargetPath(rootData);
7671

77-
return $"An exception occurred mapping {sourcePath} -> {targetPath} with rule set {mapperData.RuleSet.Name}.";
72+
var mappingExceptionCreation = Expression.Call(
73+
_factoryMethod,
74+
mapperData.RuleSet.NameConstant,
75+
sourcePath.ToConstantExpression(),
76+
targetPath.ToConstantExpression(),
77+
exceptionVariable);
78+
79+
return mappingExceptionCreation;
7880
}
7981
}
8082
}

AgileMapper/MappingRuleSet.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace AgileObjects.AgileMapper
22
{
3+
using System.Linq.Expressions;
34
using DataSources;
5+
using Extensions.Internal;
46
using Members.Population;
57
using ObjectPopulation.Enumerables;
68
using ObjectPopulation.Recursion;
@@ -16,6 +18,7 @@ public MappingRuleSet(
1618
IDataSourceFactory fallbackDataSourceFactory)
1719
{
1820
Name = name;
21+
NameConstant = name.ToConstantExpression();
1922
Settings = settings;
2023
EnumerablePopulationStrategy = enumerablePopulationStrategy;
2124
RecursiveMemberMappingStrategy = recursiveMemberMappingStrategy;
@@ -25,6 +28,8 @@ public MappingRuleSet(
2528

2629
public string Name { get; }
2730

31+
public Expression NameConstant { get; }
32+
2833
public MappingRuleSetSettings Settings { get; }
2934

3035
public IEnumerablePopulationStrategy EnumerablePopulationStrategy { get; }

AgileMapper/ObjectPopulation/MappingExpressionFactoryBase.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,9 @@ private static Expression WrapInTryCatch(Expression mappingBlock, IMemberMapperD
370370
}
371371
else
372372
{
373-
var exceptionFactoryMethod = MappingException.FactoryMethod
374-
.MakeGenericMethod(mapperData.SourceType, mapperData.TargetType);
375-
376-
var mappingExceptionCreation = Expression.Call(
377-
exceptionFactoryMethod,
378-
mapperData.MappingDataObject,
379-
exceptionVariable);
380-
381-
catchBody = Expression.Throw(mappingExceptionCreation, mappingBlock.Type);
373+
catchBody = Expression.Throw(
374+
MappingException.GetFactoryMethodCall(mapperData, exceptionVariable),
375+
mappingBlock.Type);
382376
}
383377

384378
var catchBlock = Expression.Catch(exceptionVariable, catchBody);

0 commit comments

Comments
 (0)