Browse Source

Merge pull request #23630 from abpframework/MapperlyAutoObjectMappingProvider-Exception-Message

Improve exception message for missing type mapper
pull/23674/head
Alper Ebiçoğlu 5 months ago
committed by GitHub
parent
commit
4e1606696f
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 34
      framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/MapperlyAutoObjectMappingProvider.cs
  2. 13
      framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/AbpMapperlyModule_Basic_Tests.cs

34
framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/MapperlyAutoObjectMappingProvider.cs

@ -9,7 +9,6 @@ using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Data;
using Volo.Abp.ObjectExtending;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Reflection;
namespace Volo.Abp.Mapperly;
@ -62,8 +61,7 @@ public class MapperlyAutoObjectMappingProvider : IAutoObjectMappingProvider
return destination;
}
throw new AbpException($"No {TypeHelper.GetFullNameHandlingNullableAndGenerics(typeof(IAbpMapperlyMapper<TSource, TDestination>))} or" +
$" {TypeHelper.GetFullNameHandlingNullableAndGenerics(typeof(IAbpReverseMapperlyMapper<TSource, TDestination>))} was found");
throw GetNoMapperFoundException<TSource, TDestination>();
}
public virtual TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
@ -95,8 +93,34 @@ public class MapperlyAutoObjectMappingProvider : IAutoObjectMappingProvider
return destination;
}
throw new AbpException($"No {TypeHelper.GetFullNameHandlingNullableAndGenerics(typeof(IAbpMapperlyMapper<TSource, TDestination>))} or" +
$" {TypeHelper.GetFullNameHandlingNullableAndGenerics(typeof(IAbpReverseMapperlyMapper<TSource, TDestination>))} was found");
throw GetNoMapperFoundException<TSource, TDestination>();
}
protected virtual AbpException GetNoMapperFoundException<TSource, TDestination>()
{
var newLine = Environment.NewLine;
var message = "No object mapping was found for the specified source and destination types." +
newLine +
newLine +
"Mapping attempted:" +
newLine +
$"{typeof(TSource).Name} -> {typeof(TDestination).Name}" +
newLine +
$"{typeof(TSource).FullName} -> {typeof(TDestination).FullName}" +
newLine +
newLine +
"How to fix:" +
newLine +
"Define a mapping class for these types:" +
newLine +
" - Use MapperBase<TSource, TDestination> for one-way mapping." +
newLine +
" - Use TwoWayMapperBase<TDestination, TSource> for two-way mapping." +
newLine +
newLine +
"For details, see the Mapperly integration document https://abp.io/docs/latest/framework/infrastructure/object-to-object-mapping#mapperly-integration";
return new AbpException(message);
}
protected virtual bool TryToMapCollection<TSource, TDestination>(TSource source, TDestination? destination, out TDestination collectionResult)

13
framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/AbpMapperlyModule_Basic_Tests.cs

@ -106,9 +106,14 @@ public class AbpMapperlyModule_Basic_Tests : AbpIntegratedTest<MapperlyTestModul
public void Should_Throw_Exception_If_Mapper_Is_Not_Found()
{
var exception = Assert.Throws<AbpException>(() =>_objectMapper.Map<MyEntity, MyClassDto>(new MyEntity()));
exception.Message.ShouldBe("No " +
"Volo.Abp.Mapperly.IAbpMapperlyMapper<Volo.Abp.Mapperly.SampleClasses.MyEntity,Volo.Abp.Mapperly.MyClassDto> or " +
"Volo.Abp.Mapperly.IAbpReverseMapperlyMapper<Volo.Abp.Mapperly.SampleClasses.MyEntity,Volo.Abp.Mapperly.MyClassDto>" +
" was found");
exception.Message.ShouldBe("No object mapping was found for the specified source and destination types.\n\n" +
"Mapping attempted:\n" +
"MyEntity -> MyClassDto\n" +
"Volo.Abp.Mapperly.SampleClasses.MyEntity -> Volo.Abp.Mapperly.MyClassDto\n\n" +
"How to fix:\n" +
"Define a mapping class for these types:" + "\n" +
" - Use MapperBase<TSource, TDestination> for one-way mapping.\n" +
" - Use TwoWayMapperBase<TDestination, TSource> for two-way mapping.\n\n" +
"For details, see the Mapperly integration document https://abp.io/docs/latest/framework/infrastructure/object-to-object-mapping#mapperly-integration");
}
}

Loading…
Cancel
Save