using System; using System.Runtime.InteropServices; namespace MxGateway.Worker.MxAccess; public sealed class MxAccessCreationException : Exception { 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; } public string AttemptedProgId { get; } public string AttemptedClsid { get; } public string AttemptedComClassName { get; } public int? CapturedHResult => HResult == 0 ? null : HResult; public static MxAccessCreationException From(Exception exception) { return exception is MxAccessCreationException creationException ? creationException : new MxAccessCreationException(exception); } 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; } }