using System;
using System.Runtime.InteropServices;
namespace MxGateway.Worker.MxAccess;
/// Thrown when the worker fails to instantiate the MXAccess COM object.
public sealed class MxAccessCreationException : Exception
{
/// Initializes a new instance with diagnostic info from the inner exception.
/// The exception that caused the creation failure.
public MxAccessCreationException(Exception innerException)
: base(
$"Failed to create MXAccess COM object {MxAccessInteropInfo.ComClassName} ({MxAccessInteropInfo.ProgId}).",
innerException)
{
AttemptedProgId = MxAccessInteropInfo.ProgId;
AttemptedClsid = MxAccessInteropInfo.Clsid;
AttemptedComClassName = MxAccessInteropInfo.ComClassName;
HResult = innerException.HResult;
}
/// The ProgID that was attempted during COM instantiation.
public string AttemptedProgId { get; }
/// The CLSID that was attempted during COM instantiation.
public string AttemptedClsid { get; }
/// The COM class name that was attempted during instantiation.
public string AttemptedComClassName { get; }
/// The captured HResult from the instantiation failure, or null if zero.
public int? CapturedHResult => HResult == 0 ? null : HResult;
/// Wraps an exception in MxAccessCreationException if it is not already.
/// The exception to wrap.
/// An MxAccessCreationException wrapping the input exception.
public static MxAccessCreationException From(Exception exception)
{
return exception is MxAccessCreationException creationException
? creationException
: new MxAccessCreationException(exception);
}
/// Extracts the HResult from an exception, handling MXAccess and COM exceptions specially.
/// The exception to extract the HResult from.
/// The HResult value, or null if zero.
public static int? ExtractHResult(Exception exception)
{
if (exception is MxAccessCreationException creationException)
{
return creationException.CapturedHResult;
}
if (exception is COMException comException)
{
return comException.HResult;
}
return exception.HResult == 0 ? null : exception.HResult;
}
}