From: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 25 Aug 2021 02:07:14 +0000 (-0400) Subject: Fix Enum.Parse's parse failure exception to include value in error message (#57975) X-Git-Tag: accepted/tizen/unified/20220110.054933~274 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3de61d62bd1f3b93f7426b59a701d14131668f52;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Fix Enum.Parse's parse failure exception to include value in error message (#57975) Co-authored-by: Stephen Toub --- diff --git a/src/libraries/System.Private.CoreLib/src/System/Enum.cs b/src/libraries/System.Private.CoreLib/src/System/Enum.cs index ad0a684..6274258 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Enum.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Enum.cs @@ -879,6 +879,8 @@ namespace System private static bool TryParseByName(RuntimeType enumType, ReadOnlySpan value, bool ignoreCase, bool throwOnFailure, out ulong result) { + ReadOnlySpan originalValue = value; + // Find the field. Let's assume that these are always static classes because the class is an enum. EnumInfo enumInfo = GetEnumInfo(enumType); string[] enumNames = enumInfo.Names; @@ -952,7 +954,7 @@ namespace System if (throwOnFailure) { - throw new ArgumentException(SR.Format(SR.Arg_EnumValueNotFound, value.ToString())); + throw new ArgumentException(SR.Format(SR.Arg_EnumValueNotFound, originalValue.ToString())); } result = 0; diff --git a/src/libraries/System.Runtime/tests/System/EnumTests.cs b/src/libraries/System.Runtime/tests/System/EnumTests.cs index f718e6e..a764555 100644 --- a/src/libraries/System.Runtime/tests/System/EnumTests.cs +++ b/src/libraries/System.Runtime/tests/System/EnumTests.cs @@ -288,6 +288,15 @@ namespace System.Tests } [Theory] + [InlineData("Yellow")] + [InlineData("Yellow,Orange")] + public static void Parse_NonExistentValue_IncludedInErrorMessage(string value) + { + ArgumentException e = Assert.Throws(() => Enum.Parse(typeof(SimpleEnum), value)); + Assert.Contains(value, e.Message); + } + + [Theory] [InlineData(SByteEnum.Min, "Min")] [InlineData(SByteEnum.One, "One")] [InlineData(SByteEnum.Two, "Two")]