Fix some exception messages for BindHandle-like cases (#66326)
authorKoundinya Veluri <kouvel@users.noreply.github.com>
Wed, 9 Mar 2022 02:35:55 +0000 (18:35 -0800)
committerGitHub <noreply@github.com>
Wed, 9 Mar 2022 02:35:55 +0000 (18:35 -0800)
* Fix some exception messages for BindHandle-like cases

- Updated `ThrowHelper.ThrowApplicationException` to get and include an error message
- Enabled the disabled tests, disabled a `BindHandle` test on Unixes where it now throws `PNSE` before `ArgumentNullException`

Fixes https://github.com/dotnet/runtime/issues/66273
Fixes https://github.com/dotnet/runtime/issues/66274

src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs
src/tests/baseservices/threading/threadpool/bindhandle/bindhandle1.cs
src/tests/baseservices/threading/threadpool/bindhandle/bindhandleinvalid.cs
src/tests/baseservices/threading/threadpool/bindhandle/bindhandleinvalid3.cs
src/tests/baseservices/threading/threadpool/bindhandle/bindhandleinvalid4.cs
src/tests/baseservices/threading/threadpool/bindhandle/bindhandleinvalid5.cs
src/tests/baseservices/threading/threadpool/bindhandle/bindhandleinvalid6.cs
src/tests/baseservices/threading/threadpool/bindhandle/bindhandlenull.csproj
src/tests/issues.targets

index 406ec08..c743da9 100644 (file)
@@ -41,6 +41,7 @@ using System.Diagnostics.CodeAnalysis;
 using System.IO;
 using System.Numerics;
 using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
 using System.Runtime.Intrinsics;
 using System.Runtime.Serialization;
 
@@ -533,7 +534,17 @@ namespace System
         [DoesNotReturn]
         internal static void ThrowApplicationException(int hr)
         {
-            var ex = new ApplicationException();
+            // Get a message for this HR
+            Exception? ex = Marshal.GetExceptionForHR(hr);
+            if (ex != null && !string.IsNullOrEmpty(ex.Message))
+            {
+                ex = new ApplicationException(ex.Message);
+            }
+            else
+            {
+                ex = new ApplicationException();
+            }
+
             ex.HResult = hr;
             throw ex;
         }
index 9fe2a79..4e745e7 100644 (file)
@@ -45,7 +45,7 @@ class BindHandle1
             }
             catch (Exception ex)
             {
-                Console.WriteLine("Unexpected exception: {0}", ex);
+                Console.WriteLine($"Unexpected exception - HResult: 0x{ex.HResult:x}, Exception: {ex}");
                 return (98);
             }
         }
index 6117a75..b320617 100644 (file)
@@ -22,14 +22,14 @@ class BindHandleInvalid
         }
         catch (Exception ex)
         {
-            if (ex.ToString().IndexOf("0x80070006") != -1) // E_HANDLE, we can't access hresult
+            if ((uint)ex.HResult == (uint)0x80070006) // E_HANDLE, we can't access hresult
             {
                 Console.WriteLine("Test passed");
                 return (100);
             }
             else
             {
-                Console.WriteLine("Got wrong error: {0}", ex);
+                Console.WriteLine($"Got wrong error - HResult: 0x{ex.HResult:x}, Exception: {ex}");
             }
         }
         Console.WriteLine("Didn't get argument null exception");
index 4cce05c..b9e747e 100644 (file)
@@ -35,14 +35,14 @@ class BindHandleInvalid3
             }
             catch (Exception ex)
             {
-                if (ex.ToString().IndexOf("0x80070057") != -1) // E_INVALIDARG, the handle isn't overlapped
+                if ((uint)ex.HResult == (uint)0x80070057) // E_INVALIDARG, the handle isn't overlapped
                 {
                     Console.WriteLine("Test passed");
                     return (100);
                 }
                 else
                 {
-                    Console.WriteLine("Got wrong error: {0}", ex);
+                    Console.WriteLine($"Got wrong error - HResult: 0x{ex.HResult:x}, Exception: {ex}");
                 }
             }
         }
index 1a427ea..43ff2fb 100644 (file)
@@ -30,14 +30,14 @@ class BindHandleInvalid3
             }
             catch (Exception ex)
             {
-                if (ex.ToString().IndexOf("0x80070057") != -1) // E_INVALIDARG, the handle isn't overlapped
+                if ((uint)ex.HResult == (uint)0x80070057) // E_INVALIDARG, the handle isn't overlapped
                 {
                     Console.WriteLine("Test passed");
                     return (100);
                 }
                 else
                 {
-                    Console.WriteLine("Got wrong error: {0}", ex);
+                    Console.WriteLine($"Got wrong error - HResult: 0x{ex.HResult:x}, Exception: {ex}");
                 }
             }
         }
index d4bd6eb..875e0dc 100644 (file)
@@ -36,14 +36,14 @@ class BindHandleInvalid3
             }
             catch (Exception ex)
             {
-                if (ex.ToString().IndexOf("0x80070057") != -1) // E_INVALIDARG, we've already bound the handle.
+                if ((uint)ex.HResult == (uint)0x80070057) // E_INVALIDARG, we've already bound the handle.
                 {
                     Console.WriteLine("Test passed");
                     return (100);
                 }
                 else
                 {
-                    Console.WriteLine("Got wrong error: {0}", ex);
+                    Console.WriteLine($"Got wrong error - HResult: 0x{ex.HResult:x}, Exception: {ex}");
                 }
             }
         }
index 9560ac1..6ffbfba 100644 (file)
@@ -43,7 +43,7 @@ class BindHandle1
                     }
                     catch (Exception e)
                     {
-                        Console.WriteLine("Unexpected exception on 1st call: {0}", e);
+                        Console.WriteLine($"Unexpected exception on 1st call - HResult: 0x{e.HResult:x}, Exception: {e}");
                         return (92);
                     }
 
@@ -52,14 +52,14 @@ class BindHandle1
             }
             catch (Exception ex)
             {
-                if (ex.ToString().IndexOf("0x80070057") != -1) // E_INVALIDARG, we've already bound the handle.
+                if ((uint)ex.HResult == (uint)0x80070057) // E_INVALIDARG, we've already bound the handle.
                 {
                     Console.WriteLine("Test passed");
                     return (100);
                 }
                 else
                 {
-                    Console.WriteLine("Got wrong error: {0}", ex);
+                    Console.WriteLine($"Got wrong error - HResult: 0x{ex.HResult:x}, Exception: {ex}");
                 }
             }
         }
index 81aa96a..b76ee45 100644 (file)
@@ -2,6 +2,8 @@
   <PropertyGroup>
     <OutputType>Exe</OutputType>
     <CLRTestPriority>1</CLRTestPriority>
+    <!-- Test unsupported outside of windows -->
+    <CLRTestTargetUnsupported Condition="'$(TargetsWindows)' != 'true'">true</CLRTestTargetUnsupported>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="bindhandlenull.cs" />
index 15498c2..27c7631 100644 (file)
         <ExcludeList Include="$(XunitTestBinBase)/baseservices/mono/runningmono/*">
             <Issue>This test is to verify we are running mono, and therefore only makes sense on mono.</Issue>
         </ExcludeList>
-        <ExcludeList Include="$(XUnitTestBinBase)/baseservices/threading/threadpool/bindhandle/bindhandleinvalid/*">
-            <Issue>https://github.com/dotnet/runtime/issues/66273</Issue>
-        </ExcludeList>
-        <ExcludeList Include="$(XUnitTestBinBase)/baseservices/threading/threadpool/bindhandle/bindhandleinvalid3/*">
-            <Issue>https://github.com/dotnet/runtime/issues/66273</Issue>
-        </ExcludeList>
-        <ExcludeList Include="$(XUnitTestBinBase)/baseservices/threading/threadpool/bindhandle/bindhandleinvalid4/*">
-            <Issue>https://github.com/dotnet/runtime/issues/66273</Issue>
-        </ExcludeList>
-        <ExcludeList Include="$(XUnitTestBinBase)/baseservices/threading/threadpool/bindhandle/bindhandleinvalid5/*">
-            <Issue>https://github.com/dotnet/runtime/issues/66273</Issue>
-        </ExcludeList>
-        <ExcludeList Include="$(XUnitTestBinBase)/baseservices/threading/threadpool/bindhandle/bindhandleinvalid6/*">
-            <Issue>https://github.com/dotnet/runtime/issues/66273</Issue>
-        </ExcludeList>
-        <ExcludeList Include="$(XUnitTestBinBase)/baseservices/threading/threadpool/bindhandle/bindhandlenull/*">
-            <Issue>https://github.com/dotnet/runtime/issues/66274</Issue>
-        </ExcludeList>
         <ExcludeList Include="$(XunitTestBinBase)/GC/Coverage/271010/**">
             <Issue>https://github.com/dotnet/runtime/issues/5933</Issue>
         </ExcludeList>