diff --git a/src/ZB.MOM.WW.MxGateway.Server/Grpc/MxAccessGatewayService.cs b/src/ZB.MOM.WW.MxGateway.Server/Grpc/MxAccessGatewayService.cs
index e5cab459..2028932c 100644
--- a/src/ZB.MOM.WW.MxGateway.Server/Grpc/MxAccessGatewayService.cs
+++ b/src/ZB.MOM.WW.MxGateway.Server/Grpc/MxAccessGatewayService.cs
@@ -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);
diff --git a/src/ZB.MOM.WW.MxGateway.Server/Sessions/GatewaySession.cs b/src/ZB.MOM.WW.MxGateway.Server/Sessions/GatewaySession.cs
index 6894e511..e723dd1d 100644
--- a/src/ZB.MOM.WW.MxGateway.Server/Sessions/GatewaySession.cs
+++ b/src/ZB.MOM.WW.MxGateway.Server/Sessions/GatewaySession.cs
@@ -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)
diff --git a/src/ZB.MOM.WW.MxGateway.Server/Sessions/SparseArrayException.cs b/src/ZB.MOM.WW.MxGateway.Server/Sessions/SparseArrayException.cs
new file mode 100644
index 00000000..a29760d0
--- /dev/null
+++ b/src/ZB.MOM.WW.MxGateway.Server/Sessions/SparseArrayException.cs
@@ -0,0 +1,16 @@
+namespace ZB.MOM.WW.MxGateway.Server.Sessions;
+
+///
+/// Thrown by 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
+/// InvalidArgument (see MxAccessGatewayService.MapException).
+///
+internal sealed class SparseArrayException : Exception
+{
+ /// Initializes a new instance of the class.
+ /// A description of why the sparse payload is invalid.
+ public SparseArrayException(string message)
+ : base(message)
+ {
+ }
+}
diff --git a/src/ZB.MOM.WW.MxGateway.Server/Sessions/SparseArrayExpander.cs b/src/ZB.MOM.WW.MxGateway.Server/Sessions/SparseArrayExpander.cs
index c8f81eb3..ec38ce7b 100644
--- a/src/ZB.MOM.WW.MxGateway.Server/Sessions/SparseArrayExpander.cs
+++ b/src/ZB.MOM.WW.MxGateway.Server/Sessions/SparseArrayExpander.cs
@@ -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 backstop is the only bound in test/unit-construction
/// paths that do not thread the configured cap.
///
- ///
- /// when the sparse payload is invalid: zero
+ ///
+ /// Thrown (mapped to gRPC InvalidArgument by the gRPC layer) when the sparse payload is invalid: zero
/// total length, a total length exceeding , 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);
}
diff --git a/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Grpc/MxAccessGatewayServiceTests.cs b/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Grpc/MxAccessGatewayServiceTests.cs
index 8ee15aa2..5cebc7a2 100644
--- a/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Grpc/MxAccessGatewayServiceTests.cs
+++ b/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Grpc/MxAccessGatewayServiceTests.cs
@@ -161,6 +161,28 @@ public sealed class MxAccessGatewayServiceTests
Assert.Equal(0, sessionManager.InvokeCount);
}
+ ///
+ /// Verifies that an invalid sparse-array write (a Sessions-layer SparseArrayException) surfaces
+ /// to gRPC clients as InvalidArgument rather than Unavailable (mxaccessgw#190).
+ ///
+ /// A task that represents the asynchronous operation.
+ [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(
+ async () => await service.Invoke(CreatePingRequest("session-1"), new TestServerCallContext()));
+
+ Assert.Equal(StatusCode.InvalidArgument, exception.StatusCode);
+ Assert.Contains("total_length", exception.Status.Detail);
+ }
+
/// Verifies that Invoke returns HResult status and method payload from worker reply.
/// A task that represents the asynchronous operation.
[Fact]
diff --git a/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Sessions/SparseArrayExpanderTests.cs b/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Sessions/SparseArrayExpanderTests.cs
index 2f15957e..8a658c4d 100644
--- a/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Sessions/SparseArrayExpanderTests.cs
+++ b/src/ZB.MOM.WW.MxGateway.Tests/Gateway/Sessions/SparseArrayExpanderTests.cs
@@ -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);
}
- /// Verifies that expanding a sparse array with a zero total length throws an invalid-argument .
+ /// Verifies that expanding a sparse array with a zero total length throws a .
[Fact]
public void Expand_ZeroTotalLength_Throws()
{
MxValue value = SparseValue(MxDataType.Integer, 0);
- RpcException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
- Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
+ SparseArrayException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
}
- /// Verifies that an element index at or beyond the total length throws an invalid-argument .
+ /// Verifies that an element index at or beyond the total length throws a .
[Fact]
public void Expand_IndexOutOfRange_Throws()
{
@@ -75,11 +73,10 @@ public sealed class SparseArrayExpanderTests
2,
(5, new MxValue { Int32Value = 1 }));
- RpcException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
- Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
+ SparseArrayException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
}
- /// Verifies that two sparse elements sharing the same index throw an invalid-argument .
+ /// Verifies that two sparse elements sharing the same index throw a .
[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(() => SparseArrayExpander.Expand(value));
- Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
+ SparseArrayException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
}
- /// Verifies that an unspecified element data type throws an invalid-argument .
+ /// Verifies that an unspecified element data type throws a .
[Fact]
public void Expand_UnsupportedElementType_Throws()
{
MxValue value = SparseValue(MxDataType.Unspecified, 2);
- RpcException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
- Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
+ SparseArrayException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
}
- /// Verifies that an element value whose kind does not match the declared element data type throws an invalid-argument .
+ /// Verifies that an element value whose kind does not match the declared element data type throws a .
[Fact]
public void Expand_ElementValueKindMismatch_Throws()
{
@@ -112,8 +107,7 @@ public sealed class SparseArrayExpanderTests
2,
(0, new MxValue { StringValue = "nope" }));
- RpcException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
- Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
+ SparseArrayException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
}
/// Verifies that expanding a String sparse array fills unset indices with an empty string default.
@@ -213,14 +207,13 @@ public sealed class SparseArrayExpanderTests
Assert.Throws(() => SparseArrayExpander.Expand(null!));
}
- /// Verifies that a total length exceeding the maximum supported array length throws an invalid-argument .
+ /// Verifies that a total length exceeding the maximum supported array length throws a .
[Fact]
public void Expand_TotalLengthExceedsMaxArrayLength_Throws()
{
MxValue value = SparseValue(MxDataType.Integer, 2_147_483_648u);
- RpcException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
- Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
+ SparseArrayException ex = Assert.Throws(() => SparseArrayExpander.Expand(value));
}
/// Verifies that a total length above the configured cap throws before the full array is allocated.
@@ -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(() => 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(() => 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);