using Grpc.Core;
namespace MxGateway.Client;
///
/// Maps low-level s raised by the gRPC stack to the client's
/// native exception hierarchy. Shared by every gateway and Galaxy Repository transport
/// so the gRPC-to-native translation has exactly one implementation.
///
internal static class RpcExceptionMapper
{
///
/// Translates a into the most specific native exception type.
///
/// The gRPC exception to translate.
///
/// The cancellation token of the originating call; used to distinguish a caller-driven
/// cancellation from a server-side status.
///
///
/// An when the call was cancelled, a typed
/// authentication/authorization exception for auth statuses, or an
/// carrying the originating gRPC .
///
public static Exception Map(
RpcException exception,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(exception);
if (cancellationToken.IsCancellationRequested || exception.StatusCode == StatusCode.Cancelled)
{
return new OperationCanceledException(
exception.Status.Detail,
exception,
cancellationToken);
}
return exception.StatusCode switch
{
StatusCode.Unauthenticated => new MxGatewayAuthenticationException(
exception.Status.Detail,
statusCode: exception.StatusCode,
innerException: exception),
StatusCode.PermissionDenied => new MxGatewayAuthorizationException(
exception.Status.Detail,
statusCode: exception.StatusCode,
innerException: exception),
_ => new MxGatewayException(
exception.Status.Detail,
exception.StatusCode,
exception),
};
}
}