refactor(server): SparseArrayExpander throws a domain exception, mapped to InvalidArgument in the gRPC layer (mxaccessgw#190)

Adds SparseArrayException so the Sessions layer no longer throws Grpc.Core.RpcException;
MxAccessGatewayService.MapException maps it to StatusCode.InvalidArgument (client-visible
behaviour unchanged). Adds a service-level mapping test.
This commit is contained in:
Joseph Doherty
2026-11-09 12:08:36 -05:00
parent 8ba5667b7a
commit 0d156b56cc
6 changed files with 63 additions and 30 deletions
@@ -1082,6 +1082,11 @@ public sealed class MxAccessGatewayService(
return MapSessionException(sessionException);
}
if (exception is SparseArrayException sparseException)
{
return new RpcException(new Status(StatusCode.InvalidArgument, sparseException.Message));
}
if (exception is WorkerClientException workerClientException)
{
return MapWorkerClientException(workerClientException);
@@ -1392,9 +1392,9 @@ public sealed class GatewaySession
// lands on the same MxCommand instance forwarded to the worker.
// 2. Sparse array write values are expanded to whole-array values, because MXAccess has no
// partial-array write primitive — the worker only ever sees a full MxArray.
// SparseArrayExpander.Expand throws RpcException(InvalidArgument) for an invalid sparse payload;
// that propagates out of InvokeAsync as the desired client-facing error and is deliberately not
// caught here.
// SparseArrayExpander.Expand throws SparseArrayException for an invalid sparse payload; that
// propagates out of InvokeAsync and the gRPC layer maps it to InvalidArgument. It is
// deliberately not caught here.
private void NormalizeOutboundCommand(MxCommand command)
{
switch (command.PayloadCase)
@@ -0,0 +1,16 @@
namespace ZB.MOM.WW.MxGateway.Server.Sessions;
/// <summary>
/// Thrown by <see cref="SparseArrayExpander"/> when a client-supplied sparse array write is invalid.
/// A domain exception so the Sessions layer stays free of gRPC types; the gRPC layer maps it to
/// <c>InvalidArgument</c> (see <c>MxAccessGatewayService.MapException</c>).
/// </summary>
internal sealed class SparseArrayException : Exception
{
/// <summary>Initializes a new instance of the <see cref="SparseArrayException"/> class.</summary>
/// <param name="message">A description of why the sparse payload is invalid.</param>
public SparseArrayException(string message)
: base(message)
{
}
}
@@ -1,5 +1,4 @@
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
namespace ZB.MOM.WW.MxGateway.Server.Sessions;
@@ -40,8 +39,8 @@ internal static class SparseArrayExpander
/// so the <see cref="Array.MaxLength"/> backstop is the only bound in test/unit-construction
/// paths that do not thread the configured cap.
/// </param>
/// <exception cref="RpcException">
/// <see cref="StatusCode.InvalidArgument"/> when the sparse payload is invalid: zero
/// <exception cref="SparseArrayException">
/// Thrown (mapped to gRPC <c>InvalidArgument</c> by the gRPC layer) when the sparse payload is invalid: zero
/// total length, a total length exceeding <paramref name="maxSparseArrayLength"/>, an index
/// at or beyond the total length, a duplicate index, an unsupported element type, or an
/// element value whose kind does not match the declared element type.
@@ -303,6 +302,5 @@ internal static class SparseArrayExpander
private static long ReadInt64(MxValue value) =>
value.KindCase == MxValue.KindOneofCase.Int64Value ? value.Int64Value : value.Int32Value;
private static RpcException Invalid(string message) =>
new(new Status(StatusCode.InvalidArgument, message));
private static SparseArrayException Invalid(string message) => new(message);
}
@@ -161,6 +161,28 @@ public sealed class MxAccessGatewayServiceTests
Assert.Equal(0, sessionManager.InvokeCount);
}
/// <summary>
/// Verifies that an invalid sparse-array write (a Sessions-layer <c>SparseArrayException</c>) surfaces
/// to gRPC clients as <c>InvalidArgument</c> rather than <c>Unavailable</c> (mxaccessgw#190).
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Invoke_WhenSparseArrayInvalid_MapsToInvalidArgument()
{
FakeSessionManager sessionManager = new()
{
InvokeException = new SparseArrayException("Sparse array total_length must be greater than zero."),
};
sessionManager.SeedSession(CreateSession("session-1", processId: 1234));
MxAccessGatewayService service = CreateService(sessionManager);
RpcException exception = await Assert.ThrowsAsync<RpcException>(
async () => await service.Invoke(CreatePingRequest("session-1"), new TestServerCallContext()));
Assert.Equal(StatusCode.InvalidArgument, exception.StatusCode);
Assert.Contains("total_length", exception.Status.Detail);
}
/// <summary>Verifies that Invoke returns HResult status and method payload from worker reply.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
@@ -1,5 +1,4 @@
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.MxGateway.Server.Sessions;
@@ -56,17 +55,16 @@ public sealed class SparseArrayExpanderTests
Assert.Equal(new[] { false, false, false }, value.ArrayValue.BoolValues.Values);
}
/// <summary>Verifies that expanding a sparse array with a zero total length throws an invalid-argument <see cref="RpcException"/>.</summary>
/// <summary>Verifies that expanding a sparse array with a zero total length throws a <see cref="SparseArrayException"/>.</summary>
[Fact]
public void Expand_ZeroTotalLength_Throws()
{
MxValue value = SparseValue(MxDataType.Integer, 0);
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
}
/// <summary>Verifies that an element index at or beyond the total length throws an invalid-argument <see cref="RpcException"/>.</summary>
/// <summary>Verifies that an element index at or beyond the total length throws a <see cref="SparseArrayException"/>.</summary>
[Fact]
public void Expand_IndexOutOfRange_Throws()
{
@@ -75,11 +73,10 @@ public sealed class SparseArrayExpanderTests
2,
(5, new MxValue { Int32Value = 1 }));
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
}
/// <summary>Verifies that two sparse elements sharing the same index throw an invalid-argument <see cref="RpcException"/>.</summary>
/// <summary>Verifies that two sparse elements sharing the same index throw a <see cref="SparseArrayException"/>.</summary>
[Fact]
public void Expand_DuplicateIndex_Throws()
{
@@ -89,21 +86,19 @@ public sealed class SparseArrayExpanderTests
(1, new MxValue { Int32Value = 1 }),
(1, new MxValue { Int32Value = 2 }));
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
}
/// <summary>Verifies that an unspecified element data type throws an invalid-argument <see cref="RpcException"/>.</summary>
/// <summary>Verifies that an unspecified element data type throws a <see cref="SparseArrayException"/>.</summary>
[Fact]
public void Expand_UnsupportedElementType_Throws()
{
MxValue value = SparseValue(MxDataType.Unspecified, 2);
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
}
/// <summary>Verifies that an element value whose kind does not match the declared element data type throws an invalid-argument <see cref="RpcException"/>.</summary>
/// <summary>Verifies that an element value whose kind does not match the declared element data type throws a <see cref="SparseArrayException"/>.</summary>
[Fact]
public void Expand_ElementValueKindMismatch_Throws()
{
@@ -112,8 +107,7 @@ public sealed class SparseArrayExpanderTests
2,
(0, new MxValue { StringValue = "nope" }));
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
}
/// <summary>Verifies that expanding a String sparse array fills unset indices with an empty string default.</summary>
@@ -213,14 +207,13 @@ public sealed class SparseArrayExpanderTests
Assert.Throws<ArgumentNullException>(() => SparseArrayExpander.Expand(null!));
}
/// <summary>Verifies that a total length exceeding the maximum supported array length throws an invalid-argument <see cref="RpcException"/>.</summary>
/// <summary>Verifies that a total length exceeding the maximum supported array length throws a <see cref="SparseArrayException"/>.</summary>
[Fact]
public void Expand_TotalLengthExceedsMaxArrayLength_Throws()
{
MxValue value = SparseValue(MxDataType.Integer, 2_147_483_648u);
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
}
/// <summary>Verifies that a total length above the configured cap throws <see cref="StatusCode.InvalidArgument"/> before the full array is allocated.</summary>
@@ -231,9 +224,8 @@ public sealed class SparseArrayExpanderTests
// so only the configured cap can reject it (the Array.MaxLength backstop would not).
MxValue value = SparseValue(MxDataType.Integer, 500_000_000u);
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value, maxSparseArrayLength: 1_000_000));
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
Assert.Contains("MaxSparseArrayLength", ex.Status.Detail, StringComparison.Ordinal);
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value, maxSparseArrayLength: 1_000_000));
Assert.Contains("MaxSparseArrayLength", ex.Message, StringComparison.Ordinal);
// The value must be untouched — expansion (allocation) never ran.
Assert.Equal(MxValue.KindOneofCase.SparseArrayValue, value.KindCase);