using Grpc.Core; namespace ZB.MOM.WW.MxGateway.Client; /// /// Maps a transport-level to the client's exception types. Shared by /// the session and Galaxy Repository transports so the two cannot drift apart. /// internal static class GrpcStatusMapper { /// /// Maps . The original gRPC status stays reachable through /// and the inner exception. /// /// The gRPC failure. /// The caller's token. /// /// for the session API, where NotFound / FailedPrecondition /// mean the session is gone or not ready; the Galaxy Repository has no sessions. /// /// The exception to throw. public static Exception Map(RpcException exception, CancellationToken cancellationToken, bool sessionScoped) { // Only the caller's own cancellation is an OperationCanceledException. A Cancelled status // with a live token was produced by the gateway (session closed or killed, gateway // shutdown) and must not be swallowed by `catch (OperationCanceledException)` shutdown code. if (cancellationToken.IsCancellationRequested) { return new OperationCanceledException( exception.Status.Detail, exception, cancellationToken); } return exception.StatusCode switch { StatusCode.Unauthenticated => new MxGatewayAuthenticationException( exception.Status.Detail, innerException: exception), StatusCode.PermissionDenied => new MxGatewayAuthorizationException( exception.Status.Detail, innerException: exception), StatusCode.ResourceExhausted => new MxGatewayResourceExhaustedException( exception.Status.Detail, MxGatewayResourceExhaustedException.ReadReason(exception), exception), StatusCode.NotFound or StatusCode.FailedPrecondition when sessionScoped => new MxGatewaySessionException( exception.Status.Detail, innerException: exception), _ => new MxGatewayException(exception.Status.Detail, exception), }; } }