Issue #31: implement mxstatus proxy and hresult conversion
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using MxGateway.Contracts.Proto;
|
||||
|
||||
namespace MxGateway.Worker.Conversion;
|
||||
|
||||
public sealed class HResultConverter
|
||||
{
|
||||
public HResultConversion Convert(Exception exception)
|
||||
{
|
||||
if (exception is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(exception));
|
||||
}
|
||||
|
||||
int hresult = exception is COMException comException
|
||||
? comException.ErrorCode
|
||||
: exception.HResult;
|
||||
|
||||
return new HResultConversion(
|
||||
hresult,
|
||||
CreateProtocolStatus(hresult, exception),
|
||||
CreateSafeDiagnosticMessage(exception));
|
||||
}
|
||||
|
||||
public ProtocolStatus CreateProtocolStatus(
|
||||
int hresult,
|
||||
Exception? exception = null)
|
||||
{
|
||||
return new ProtocolStatus
|
||||
{
|
||||
Code = hresult == 0 ? ProtocolStatusCode.Ok : ProtocolStatusCode.MxaccessFailure,
|
||||
Message = exception is null
|
||||
? FormatHResult(hresult)
|
||||
: $"{exception.GetType().Name}: {FormatHResult(hresult)}",
|
||||
};
|
||||
}
|
||||
|
||||
private static string CreateSafeDiagnosticMessage(Exception exception)
|
||||
{
|
||||
return $"{exception.GetType().FullName}: {FormatHResult(exception.HResult)}";
|
||||
}
|
||||
|
||||
private static string FormatHResult(int hresult)
|
||||
{
|
||||
return $"HRESULT 0x{unchecked((uint)hresult):X8}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user