Skip to content

Commit 7cda102

Browse files
committed
Use ThrowIfFailedOnDebug
1 parent 7b952b5 commit 7cda102

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
using System.Runtime.InteropServices;
5+
using Windows.Win32.Foundation;
6+
7+
namespace Windows.Win32
8+
{
9+
public static class HRESULTExtensions
10+
{
11+
/// <summary>
12+
/// Throws an exception if the <see cref="HRESULT"/> indicates a failure in debug mode. Otherwise, it returns the original <see cref="HRESULT"/>.
13+
/// </summary>
14+
/// <param name="hr">Represents the result of an operation, indicating success or failure.</param>
15+
/// <returns>Returns the original <see cref="HRESULT"/> value regardless of the operation's success.</returns>
16+
public static HRESULT ThrowIfFailedOnDebug(this HRESULT hr)
17+
{
18+
#if DEBUG
19+
if (hr.Failed)
20+
Marshal.ThrowExceptionForHR(hr.Value);
21+
#endif
22+
23+
return hr;
24+
}
25+
}
26+
}

src/Files.App/Utils/Storage/Helpers/SyncRootHelpers.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Win32;
55
using System.Runtime.CompilerServices;
66
using Windows.Win32;
7+
using Windows.Win32.Foundation;
78
using Windows.Win32.System.Com;
89
using Windows.Win32.System.WinRT;
910
using WinRT;
@@ -19,30 +20,30 @@ private static unsafe (bool Success, ulong Capacity, ulong Used) GetSyncRootQuot
1920
!Guid.TryParse(factoryClsidString, out var factoryClsid))
2021
return (false, 0, 0);
2122

23+
HRESULT hr = default;
2224
ulong ulTotalSize = 0ul, ulUsedSize = 0ul;
2325
using ComPtr<IStorageProviderStatusUISourceFactory> pStorageProviderStatusUISourceFactory = default;
2426
using ComPtr<IStorageProviderStatusUISource> pStorageProviderStatusUISource = default;
2527
using ComPtr<IStorageProviderStatusUI> pStorageProviderStatusUI = default;
2628
using ComPtr<IStorageProviderQuotaUI> pStorageProviderQuotaUI = default;
2729

28-
var hr = PInvoke.CoCreateInstance(
30+
if (PInvoke.CoCreateInstance(
2931
&factoryClsid,
3032
null,
3133
CLSCTX.CLSCTX_LOCAL_SERVER,
3234
(Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in IStorageProviderStatusUISourceFactory.Guid)),
33-
(void**)pStorageProviderStatusUISourceFactory.GetAddressOf());
34-
if (hr.Failed)
35+
(void**)pStorageProviderStatusUISourceFactory.GetAddressOf()).ThrowIfFailedOnDebug().Failed)
3536
return (false, 0, 0);
3637

3738
var syncRootIdHString = new MarshalString.Pinnable(syncRootId);
3839
fixed (char* pSyncRootIdHString = syncRootIdHString)
3940
{
40-
hr = pStorageProviderStatusUISourceFactory.Get()->GetStatusUISource(syncRootIdHString.GetAbi(), pStorageProviderStatusUISource.GetAddressOf()).ThrowOnFailure();
41-
hr = pStorageProviderStatusUISource.Get()->GetStatusUI(pStorageProviderStatusUI.GetAddressOf()).ThrowOnFailure();
42-
hr = pStorageProviderStatusUI.Get()->GetQuotaUI(pStorageProviderQuotaUI.GetAddressOf()).ThrowOnFailure();
43-
44-
hr = pStorageProviderQuotaUI.Get()->GetQuotaTotalInBytes(&ulTotalSize);
45-
hr = pStorageProviderQuotaUI.Get()->GetQuotaUsedInBytes(&ulUsedSize);
41+
if (pStorageProviderStatusUISourceFactory.Get()->GetStatusUISource(syncRootIdHString.GetAbi(), pStorageProviderStatusUISource.GetAddressOf()).ThrowIfFailedOnDebug().Failed ||
42+
pStorageProviderStatusUISource.Get()->GetStatusUI(pStorageProviderStatusUI.GetAddressOf()).ThrowIfFailedOnDebug().Failed ||
43+
pStorageProviderStatusUI.Get()->GetQuotaUI(pStorageProviderQuotaUI.GetAddressOf()).ThrowIfFailedOnDebug().Failed ||
44+
pStorageProviderQuotaUI.Get()->GetQuotaTotalInBytes(&ulTotalSize).ThrowIfFailedOnDebug().Failed ||
45+
pStorageProviderQuotaUI.Get()->GetQuotaUsedInBytes(&ulUsedSize).ThrowIfFailedOnDebug().Failed)
46+
return (false, 0, 0);
4647
}
4748

4849
return (true, ulTotalSize, ulUsedSize);

0 commit comments

Comments
 (0)