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
@@ -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);