Skip to content

Commit

Permalink
Fix some exception messages for BindHandle-like cases (dotnet#66326)
Browse files Browse the repository at this point in the history
* 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 dotnet#66273
Fixes dotnet#66274
  • Loading branch information
kouvel committed Mar 9, 2022
1 parent 60f593b commit a0a5fc9
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 31 deletions.
13 changes: 12 additions & 1 deletion src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
using System.IO;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Serialization;

Expand Down Expand Up @@ -533,7 +534,17 @@ internal static void ArgumentOutOfRangeException_Enum_Value()
[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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int RunTest()
}
catch (Exception ex)
{
Console.WriteLine("Unexpected exception: {0}", ex);
Console.WriteLine($"Unexpected exception - HResult: 0x{ex.HResult:x}, Exception: {ex}");
return (98);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ int RunTest()
}
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ int RunTest()
}
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}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ int RunTest()
}
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}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ int RunTest()
}
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}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int RunTest()
}
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);
}

Expand All @@ -52,14 +52,14 @@ int RunTest()
}
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}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
18 changes: 0 additions & 18 deletions src/tests/issues.targets
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,6 @@
<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>
Expand Down

0 comments on commit a0a5fc9

Please sign in to comment.