Skip to content

Commit bafe3c3

Browse files
authored
Exception Handling Implementation in WinRT.Runtime2/InteropServices/RestrictedErrorInfo.cs (#2087)
1 parent 79fae59 commit bafe3c3

25 files changed

+1773
-509
lines changed

src/Authoring/WinRT.Host/WinRT.Host.vcxproj

Lines changed: 297 additions & 297 deletions
Large diffs are not rendered by default.
Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<ItemGroup>
4-
<Filter Include="Source Files">
5-
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6-
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7-
</Filter>
8-
<Filter Include="Header Files">
9-
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10-
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
11-
</Filter>
12-
<Filter Include="Resource Files">
13-
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14-
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15-
</Filter>
16-
</ItemGroup>
17-
<ItemGroup>
18-
<ClInclude Include="pch.h">
19-
<Filter>Header Files</Filter>
20-
</ClInclude>
21-
</ItemGroup>
22-
<ItemGroup>
23-
<ClCompile Include="pch.cpp">
24-
<Filter>Source Files</Filter>
25-
</ClCompile>
26-
<ClCompile Include="test.cpp">
27-
<Filter>Source Files</Filter>
28-
</ClCompile>
29-
</ItemGroup>
30-
<ItemGroup>
31-
<None Include="packages.config" />
32-
<None Include="Directory.Build.targets" />
33-
<None Include="WinRT.Host.runtimeconfig.json" />
34-
<None Include="DoNotImport_MsAppxPackageTargets.targets" />
35-
</ItemGroup>
36-
<ItemGroup>
37-
<Manifest Include="AuthoringConsumptionTest.exe.manifest" />
38-
</ItemGroup>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClInclude Include="pch.h">
19+
<Filter>Header Files</Filter>
20+
</ClInclude>
21+
</ItemGroup>
22+
<ItemGroup>
23+
<ClCompile Include="pch.cpp">
24+
<Filter>Source Files</Filter>
25+
</ClCompile>
26+
<ClCompile Include="test.cpp">
27+
<Filter>Source Files</Filter>
28+
</ClCompile>
29+
</ItemGroup>
30+
<ItemGroup>
31+
<None Include="packages.config" />
32+
<None Include="Directory.Build.targets" />
33+
<None Include="WinRT.Host.runtimeconfig.json" />
34+
<None Include="DoNotImport_MsAppxPackageTargets.targets" />
35+
</ItemGroup>
36+
<ItemGroup>
37+
<Manifest Include="AuthoringConsumptionTest.exe.manifest" />
38+
</ItemGroup>
3939
</Project>

src/Tests/TestComponentCSharp/ManualProjectionTestClasses.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ namespace winrt::TestComponentCSharp::implementation
3434

3535
void CustomDisposableTest::Close()
3636
{
37+
// Leaving in for testing purposes
38+
// throw winrt::hresult_access_denied();
3739
}
3840

3941
CustomBindableVectorTest::CustomBindableVectorTest()

src/Tests/TestComponentCSharp/TestComponentCSharp.vcxproj

Lines changed: 150 additions & 150 deletions
Large diffs are not rendered by default.

src/WinRT.Runtime/IInspectable.net5.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ partial class IInspectable : IWinRTObject
1515
{
1616
IObjectReference IWinRTObject.NativeObject => _obj;
1717
bool IWinRTObject.HasUnwrappableNativeObject => true;
18-
18+
1919
private volatile ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> _queryInterfaceCache;
2020
private ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> MakeQueryInterfaceCache()
2121
{
22-
System.Threading.Interlocked.CompareExchange(ref _queryInterfaceCache, new ConcurrentDictionary<RuntimeTypeHandle, IObjectReference>(), null);
22+
System.Threading.Interlocked.CompareExchange(ref _queryInterfaceCache, new ConcurrentDictionary<RuntimeTypeHandle, IObjectReference>(), null);
2323
return _queryInterfaceCache;
2424
}
2525
ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> IWinRTObject.QueryInterfaceCache => _queryInterfaceCache ?? MakeQueryInterfaceCache();
2626
private volatile ConcurrentDictionary<RuntimeTypeHandle, object> _additionalTypeData;
2727
private ConcurrentDictionary<RuntimeTypeHandle, object> MakeAdditionalTypeData()
2828
{
29-
System.Threading.Interlocked.CompareExchange(ref _additionalTypeData, new ConcurrentDictionary<RuntimeTypeHandle, object>(), null);
29+
System.Threading.Interlocked.CompareExchange(ref _additionalTypeData, new ConcurrentDictionary<RuntimeTypeHandle, object>(), null);
3030
return _additionalTypeData;
3131
}
3232
ConcurrentDictionary<RuntimeTypeHandle, object> IWinRTObject.AdditionalTypeData => _additionalTypeData ?? MakeAdditionalTypeData();
3333
}
3434

35-
}
35+
}

src/WinRT.Runtime2/ABI/System/Exception.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static Exception ConvertToUnmanaged(global::System.Exception? value)
7070
/// <returns>The managed <see cref="global::System.Exception"/> value</returns>
7171
public static global::System.Exception? ConvertToManaged(Exception value)
7272
{
73-
return RestrictedErrorInfo.GetExceptionForHR(value.Value);
73+
return RestrictedErrorInfo.GetExceptionForHR(value.Value, out _);
7474
}
7575
}
7676

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using WindowsRuntime.InteropServices;
6+
7+
namespace Microsoft.UI.Xaml;
8+
9+
/// <summary>
10+
/// Represents an exception that occurs when an element is not available.
11+
/// </summary>
12+
/// <remarks>
13+
/// Sets <see cref="Exception.HResult"/> to <see cref="WellKnownErrorCodes.E_ELEMENTNOTAVAILABLE"/>
14+
/// to enable consistent interop/error mapping.
15+
/// </remarks>
16+
internal sealed class ElementNotAvailableException : Exception
17+
{
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="ElementNotAvailableException"/> class
20+
/// with a default error message indicating the element is not available.
21+
/// </summary>
22+
public ElementNotAvailableException()
23+
: base("The element is not available.")
24+
{
25+
HResult = WellKnownErrorCodes.E_ELEMENTNOTAVAILABLE;
26+
}
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="ElementNotAvailableException"/> class
30+
/// with a specified error message.
31+
/// </summary>
32+
/// <param name="message">The error message that explains the reason for the exception.</param>
33+
public ElementNotAvailableException(string message)
34+
: base(message)
35+
{
36+
HResult = WellKnownErrorCodes.E_ELEMENTNOTAVAILABLE;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using WindowsRuntime.InteropServices;
6+
7+
namespace Microsoft.UI.Xaml;
8+
9+
/// <summary>
10+
/// Represents an exception that occurs when an element is not enabled.
11+
/// </summary>
12+
/// <remarks>
13+
/// Sets <see cref="Exception.HResult"/> to <see cref="WellKnownErrorCodes.E_ELEMENTNOTENABLED"/>
14+
/// to enable consistent interop/error mapping.
15+
/// </remarks>
16+
internal sealed class ElementNotEnabledException : Exception
17+
{
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="ElementNotEnabledException"/> class
20+
/// with a default error message indicating the element is not enabled.
21+
/// </summary>
22+
public ElementNotEnabledException()
23+
: base("The element is not enabled.")
24+
{
25+
HResult = WellKnownErrorCodes.E_ELEMENTNOTENABLED;
26+
}
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="ElementNotEnabledException"/> class
30+
/// with a specified error message.
31+
/// </summary>
32+
/// <param name="message">The error message that explains the reason for the exception.</param>
33+
public ElementNotEnabledException(string message)
34+
: base(message)
35+
{
36+
HResult = WellKnownErrorCodes.E_ELEMENTNOTENABLED;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using WindowsRuntime.InteropServices;
6+
7+
namespace Microsoft.UI.Xaml;
8+
9+
/// <summary>
10+
/// Represents an exception that occurs when a layout cycle is detected during GUI layout.
11+
/// </summary>
12+
/// <remarks>
13+
/// Sets <see cref="Exception.HResult"/> to <see cref="WellKnownErrorCodes.E_LAYOUTCYCLE"/>
14+
/// to enable consistent interop/error mapping.
15+
/// </remarks>
16+
internal sealed class LayoutCycleException : Exception
17+
{
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="LayoutCycleException"/> class
20+
/// with a default error message indicating a layout cycle occurred.
21+
/// </summary>
22+
public LayoutCycleException()
23+
: base("A cycle occurred while laying out the GUI.")
24+
{
25+
HResult = WellKnownErrorCodes.E_LAYOUTCYCLE;
26+
}
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="LayoutCycleException"/> class
30+
/// with a specified error message.
31+
/// </summary>
32+
/// <param name="message">The error message that explains the reason for the exception.</param>
33+
public LayoutCycleException(string message)
34+
: base(message)
35+
{
36+
HResult = WellKnownErrorCodes.E_LAYOUTCYCLE;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using WindowsRuntime.InteropServices;
6+
7+
namespace Microsoft.UI.Xaml;
8+
9+
/// <summary>
10+
/// Represents an exception that occurs when XAML parsing fails.
11+
/// </summary>
12+
/// <remarks>
13+
/// Sets <see cref="Exception.HResult"/> to <see cref="WellKnownErrorCodes.E_XAMLPARSEFAILED"/>
14+
/// to enable consistent interop/error mapping.
15+
/// </remarks>
16+
internal sealed class XamlParseException : Exception
17+
{
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="XamlParseException"/> class
20+
/// with a default error message indicating XAML parsing failure.
21+
/// </summary>
22+
public XamlParseException()
23+
: base("XAML parsing failed.")
24+
{
25+
HResult = WellKnownErrorCodes.E_XAMLPARSEFAILED;
26+
}
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="XamlParseException"/> class
30+
/// with a specified error message.
31+
/// </summary>
32+
/// <param name="message">The error message that explains the reason for the exception.</param>
33+
public XamlParseException(string message)
34+
: base(message)
35+
{
36+
HResult = WellKnownErrorCodes.E_XAMLPARSEFAILED;
37+
}
38+
}

0 commit comments

Comments
 (0)