diff --git a/src/Riok.Mapperly/Descriptors/Mappings/EnumNameMapping.cs b/src/Riok.Mapperly/Descriptors/Mappings/EnumNameMapping.cs index 55c3c22e06..3e126c431c 100644 --- a/src/Riok.Mapperly/Descriptors/Mappings/EnumNameMapping.cs +++ b/src/Riok.Mapperly/Descriptors/Mappings/EnumNameMapping.cs @@ -25,10 +25,10 @@ public EnumNameMapping( public override IEnumerable BuildBody(TypeMappingBuildContext ctx) { - // fallback switch arm: _ => throw new ArgumentOutOfRangeException("source"); + // fallback switch arm: _ => throw new ArgumentOutOfRangeException(nameof(source), source, message); var fallbackArm = SwitchExpressionArm( DiscardPattern(), - ThrowArgumentOutOfRangeException(ctx.Source)); + ThrowArgumentOutOfRangeException(ctx.Source, $"The value of enum {SourceType.Name} is not supported")); // switch for each name to the enum value // eg: Enum1.Value1 => Enum2.Value1, diff --git a/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs b/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs index 92d21a1b41..4d449dc366 100644 --- a/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs +++ b/src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs @@ -114,10 +114,10 @@ public static ThrowExpressionSyntax ThrowNullReferenceException(string message) .WithArgumentList(ArgumentList(StringLiteral(message)))); } - public static ThrowExpressionSyntax ThrowArgumentOutOfRangeException(ExpressionSyntax arg) + public static ThrowExpressionSyntax ThrowArgumentOutOfRangeException(ExpressionSyntax arg, string message) { return ThrowExpression(ObjectCreationExpression(IdentifierName(ArgumentOutOfRangeExceptionClassName)) - .WithArgumentList(ArgumentList(NameOf(arg)))); + .WithArgumentList(ArgumentList(NameOf(arg), arg, StringLiteral(message)))); } public static ThrowExpressionSyntax ThrowArgumentNullException(ExpressionSyntax arg) diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource.verified.cs index e1206250e4..48f0b833b9 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/MapperTest.SnapshotGeneratedSource.verified.cs @@ -153,7 +153,7 @@ public partial Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapToEnumDto Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v)), + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), }; } diff --git a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource.verified.cs b/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource.verified.cs index 7be62ef9f3..8cc4a57955 100644 --- a/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource.verified.cs +++ b/test/Riok.Mapperly.IntegrationTests/_snapshots/StaticMapperTest.SnapshotGeneratedSource.verified.cs @@ -206,7 +206,7 @@ public static partial Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName MapTo Riok.Mapperly.IntegrationTests.Models.TestEnum.Value10 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value10, Riok.Mapperly.IntegrationTests.Models.TestEnum.Value20 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value20, Riok.Mapperly.IntegrationTests.Models.TestEnum.Value30 => Riok.Mapperly.IntegrationTests.Dto.TestEnumDtoByName.Value30, - _ => throw new System.ArgumentOutOfRangeException(nameof(v)), + _ => throw new System.ArgumentOutOfRangeException(nameof(v), v, "The value of enum TestEnum is not supported"), }; } diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs index 09541034ee..38a13e5910 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumTest.cs @@ -87,7 +87,7 @@ public void EnumToOtherEnumByNameShouldSwitch() E1.B => E2.B, E1.C => E2.C, E1.E => E2.E, - _ => throw new System.ArgumentOutOfRangeException(nameof(source)), + _ => throw new System.ArgumentOutOfRangeException(nameof(source), source, ""The value of enum E1 is not supported""), };"); } @@ -110,7 +110,7 @@ public void EnumToOtherEnumByNameIgnoreCaseShouldSwitch() E1.E => E2.E, E1.f => E2.f, E1.F => E2.f, - _ => throw new System.ArgumentOutOfRangeException(nameof(source)), + _ => throw new System.ArgumentOutOfRangeException(nameof(source), source, ""The value of enum E1 is not supported""), };"); } @@ -151,7 +151,7 @@ enum E2 {A = 100, B, C} E1.A => E2.A, E1.B => E2.B, E1.C => E2.C, - _ => throw new System.ArgumentOutOfRangeException(nameof(source)), + _ => throw new System.ArgumentOutOfRangeException(nameof(source), source, ""The value of enum E1 is not supported""), };"); } diff --git a/test/Riok.Mapperly.Tests/_snapshots/EnumTest.EnumToOtherEnumByNameWithoutOverlap#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/EnumTest.EnumToOtherEnumByNameWithoutOverlap#Mapper.g.verified.cs index c7c585759b..c91c767367 100644 --- a/test/Riok.Mapperly.Tests/_snapshots/EnumTest.EnumToOtherEnumByNameWithoutOverlap#Mapper.g.verified.cs +++ b/test/Riok.Mapperly.Tests/_snapshots/EnumTest.EnumToOtherEnumByNameWithoutOverlap#Mapper.g.verified.cs @@ -6,7 +6,7 @@ private partial E2 ToE1(E1 source) { return source switch { - _ => throw new System.ArgumentOutOfRangeException(nameof(source)), + _ => throw new System.ArgumentOutOfRangeException(nameof(source), source, "The value of enum E1 is not supported"), }; } } \ No newline at end of file