Converted error types to use new ErrorHelper and hid stack mentions of ThrowIfError and related methods.

nullableenabled
David Hall 2024-04-27 12:53:47 -06:00
parent b3e390b709
commit 0d6cf66f53
3 changed files with 16 additions and 51 deletions

View File

@ -619,7 +619,7 @@ public partial struct HRESULT : IComparable, IComparable<HRESULT>, IEquatable<HR
/// </summary>
/// <param name="hresult">The 32-bit raw HRESULT value.</param>
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
[System.Diagnostics.DebuggerStepThrough]
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public static void ThrowIfFailed(HRESULT hresult, string? message = null) => hresult.ThrowIfFailed(message);
/// <summary>
@ -628,7 +628,7 @@ public partial struct HRESULT : IComparable, IComparable<HRESULT>, IEquatable<HR
/// </summary>
/// <param name="hresult">The 32-bit raw HRESULT value.</param>
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
[System.Diagnostics.DebuggerStepThrough]
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public static void ThrowIfFailed(int hresult, string? message = null) => new HRESULT(hresult).ThrowIfFailed(message);
/// <summary>Compares the current object with another object of the same type.</summary>
@ -723,7 +723,7 @@ public partial struct HRESULT : IComparable, IComparable<HRESULT>, IEquatable<HR
/// </summary>
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
[SecurityCritical, SecuritySafeCritical]
[System.Diagnostics.DebuggerStepThrough]
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public void ThrowIfFailed(string? message = null)
{
var exception = GetException(message);
@ -747,7 +747,7 @@ public partial struct HRESULT : IComparable, IComparable<HRESULT>, IEquatable<HR
}
}
}
var msg = FormatMessage(unchecked((uint)_value), StaticFieldValueHash.GetFieldLib<HRESULT, int>(_value));
var msg = ErrorHelper.GetErrorMessage<HRESULT, int>(_value);
return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", _value)) + (msg == null ? "" : ": " + msg);
}
@ -790,47 +790,6 @@ public partial struct HRESULT : IComparable, IComparable<HRESULT>, IEquatable<HR
ulong IConvertible.ToUInt64(IFormatProvider? provider) => ((IConvertible)unchecked((uint)_value)).ToUInt64(provider);
/// <summary>Formats the message.</summary>
/// <param name="id">The error.</param>
/// <param name="lib">The optional library.</param>
/// <returns>The string.</returns>
internal static string FormatMessage(uint id, string? lib = null)
{
var flags = lib is null ? 0x1200U /*FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM*/ : 0xA00U /*FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE*/;
HINSTANCE hInst = lib is null ? default : LoadLibraryEx(lib, default, 0x1002 /*LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_AS_DATAFILE*/);
var buf = new StringBuilder(1024);
try
{
do
{
if (0 != FormatMessage(flags, hInst, id, 0, buf, (uint)buf.Capacity, default))
return buf.ToString();
var lastError = Win32Error.GetLastError();
if (lastError == Win32Error.ERROR_MR_MID_NOT_FOUND || lastError == Win32Error.ERROR_MUI_FILE_NOT_FOUND || lastError == Win32Error.ERROR_RESOURCE_TYPE_NOT_FOUND)
break;
if (lastError != Win32Error.ERROR_INSUFFICIENT_BUFFER)
lastError.ThrowIfFailed();
buf.Capacity *= 2;
} while (true && buf.Capacity < 1024 * 16); // Don't go crazy
}
finally
{
if (hInst != default)
FreeLibrary(hInst);
}
return string.Empty;
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
static extern int FormatMessage(uint dwFlags, HINSTANCE lpSource, uint dwMessageId, uint dwLanguageId, StringBuilder lpBuffer, uint nSize, IntPtr Arguments);
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeLibrary([In] HINSTANCE hLibModule);
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
static extern HINSTANCE LoadLibraryEx([MarshalAs(UnmanagedType.LPTStr)] string lpLibFileName, HANDLE hFile, uint dwFlags);
}
private static int? ValueFromObj(object? obj)
{
switch (obj)

View File

@ -388,6 +388,7 @@ public partial struct NTStatus : IComparable, IComparable<NTStatus>, IEquatable<
/// </summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public static void ThrowIfFailed(int ntstatus, string? message = null) => new NTStatus(ntstatus).ThrowIfFailed(message);
/// <summary>Compares the current object with another object of the same type.</summary>
@ -461,6 +462,7 @@ public partial struct NTStatus : IComparable, IComparable<NTStatus>, IEquatable<
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
[SecurityCritical]
[SecuritySafeCritical]
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public void ThrowIfFailed(string? message = null)
{
var exception = GetException(message);
@ -482,7 +484,7 @@ public partial struct NTStatus : IComparable, IComparable<NTStatus>, IEquatable<
{
// Check for defined NTStatus value
StaticFieldValueHash.TryGetFieldName<NTStatus, int>(_value, out var err);
var msg = HRESULT.FormatMessage(unchecked((uint)_value));
var msg = ErrorHelper.GetErrorMessage<NTStatus, int>(_value);
return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", _value)) + (msg == null ? "" : ": " + msg);
}

View File

@ -86,12 +86,12 @@ public partial struct Win32Error : IEquatable<Win32Error>, IEquatable<uint>, ICo
/// <summary>Throws if failed.</summary>
/// <param name="err">The error.</param>
/// <param name="message">The message.</param>
[System.Diagnostics.DebuggerStepThrough]
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public static void ThrowIfFailed(Win32Error err, string? message = null) => err.ThrowIfFailed(message);
/// <summary>Throws the last error.</summary>
/// <param name="message">The message to associate with the exception.</param>
[System.Diagnostics.DebuggerStepThrough]
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public static void ThrowLastError(string? message = null) => GetLastError().ThrowIfFailed(message);
/// <summary>Throws the last error if the predicate delegate returns <see langword="true"/>.</summary>
@ -100,6 +100,7 @@ public partial struct Win32Error : IEquatable<Win32Error>, IEquatable<uint>, ICo
/// <param name="valueIsFailure">The delegate which returns <see langword="true"/> on failure.</param>
/// <param name="message">The message.</param>
/// <returns>The <paramref name="value"/> passed in on success.</returns>
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public static T ThrowLastErrorIf<T>(T value, Func<T, bool> valueIsFailure, string? message = null)
{
if (valueIsFailure(value))
@ -110,22 +111,25 @@ public partial struct Win32Error : IEquatable<Win32Error>, IEquatable<uint>, ICo
/// <summary>Throws the last error if the function returns <see langword="false"/>.</summary>
/// <param name="value">The value to check.</param>
/// <param name="message">The message.</param>
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public static bool ThrowLastErrorIfFalse(bool value, string? message = null) => ThrowLastErrorIf(value, v => !v, message);
/// <summary>Throws the last error if the value is an invalid handle.</summary>
/// <param name="value">The SafeHandle to check.</param>
/// <param name="message">The message.</param>
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public static T ThrowLastErrorIfInvalid<T>(T value, string? message = null) where T : SafeHandle => ThrowLastErrorIf(value, v => v.IsInvalid, message);
/// <summary>Throws the last error if the value is a NULL pointer (IntPtr.Zero).</summary>
/// <param name="value">The pointer to check.</param>
/// <param name="message">The message.</param>
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public static IntPtr ThrowLastErrorIfNull(IntPtr value, string? message = null) => ThrowLastErrorIf(value, v => v == IntPtr.Zero, message);
/// <summary>Throws if the last error failed, unless the error is the specified value.</summary>
/// <param name="exception">The failure code to ignore.</param>
/// <param name="message">The message to associate with the exception.</param>
[System.Diagnostics.DebuggerStepThrough]
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public static void ThrowLastErrorUnless(Win32Error exception, string? message = null) => GetLastError().ThrowUnless(exception, message);
/// <summary>Compares the current object with another object of the same type.</summary>
@ -187,7 +191,7 @@ public partial struct Win32Error : IEquatable<Win32Error>, IEquatable<uint>, ICo
/// <summary>Throws if failed.</summary>
/// <param name="message">The message.</param>
/// <exception cref="Win32Exception"></exception>
[System.Diagnostics.DebuggerStepThrough]
[System.Diagnostics.DebuggerStepThrough, System.Diagnostics.DebuggerHidden, System.Diagnostics.StackTraceHidden]
public void ThrowIfFailed(string? message = null)
{
if (value != ERROR_SUCCESS) throw GetException(message)!;
@ -211,7 +215,7 @@ public partial struct Win32Error : IEquatable<Win32Error>, IEquatable<uint>, ICo
public override string ToString()
{
_ = StaticFieldValueHash.TryGetFieldName<Win32Error, uint>(value, out var err);
var msg = HRESULT.FormatMessage(value);
var msg = ErrorHelper.GetErrorMessage<Win32Error, uint>(value);
return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", value)) + (msg == null ? "" : ": " + msg);
}