Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve enum mapping runtime exception #230

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Riok.Mapperly/Descriptors/Mappings/EnumNameMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public EnumNameMapping(

public override IEnumerable<StatementSyntax> 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,
Expand Down
4 changes: 2 additions & 2 deletions src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};
}

Expand Down
6 changes: 3 additions & 3 deletions test/Riok.Mapperly.Tests/Mapping/EnumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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""),
};");
}

Expand All @@ -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""),
};");
}

Expand Down Expand Up @@ -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""),
};");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};
}
}