fix(client): surface a Bad per-node HistoryRead status instead of '0 values returned'

DefaultSessionAdapter swallowed a Bad HistoryReadResult.StatusCode (raw: break;
aggregate: skip) so Client.CLI/UI printed an empty result for
BadHistoryOperationUnsupported / BadUserAccessDenied. It now throws a
ServiceResultException naming the node and status; BadNoData stays empty.

Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
Joseph Doherty
2026-10-08 23:35:13 -04:00
parent f7d497e532
commit f7f7d948cd
3 changed files with 68 additions and 4 deletions
@@ -1,6 +1,7 @@
using Opc.Ua;
using Opc.Ua.Client;
using Serilog;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers;
namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Adapters;
@@ -172,8 +173,9 @@ internal sealed class DefaultSessionAdapter : ISessionAdapter
break;
var result = results[0];
if (StatusCode.IsBad(result.StatusCode))
break;
// A refused read (BadHistoryOperationUnsupported, BadUserAccessDenied, ...) must surface; only
// BadNoData is an empty result. Swallowing it printed "0 values returned" for a refusal.
HistoryReadStatus.ThrowIfRefused(result.StatusCode, nodeId);
if (result.HistoryData is ExtensionObject ext && ext.Body is HistoryData historyData)
allValues.AddRange(historyData.DataValues);
@@ -218,8 +220,8 @@ internal sealed class DefaultSessionAdapter : ISessionAdapter
if (results != null && results.Count > 0)
{
var result = results[0];
if (!StatusCode.IsBad(result.StatusCode) &&
result.HistoryData is ExtensionObject ext &&
HistoryReadStatus.ThrowIfRefused(result.StatusCode, nodeId);
if (result.HistoryData is ExtensionObject ext &&
ext.Body is HistoryData historyData)
allValues.AddRange(historyData.DataValues);
}
@@ -0,0 +1,24 @@
using Opc.Ua;
namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers;
/// <summary>
/// Classifies the per-node <c>HistoryReadResult.StatusCode</c> a server returns for a HistoryRead.
/// A Bad per-node status (<c>BadHistoryOperationUnsupported</c>, <c>BadUserAccessDenied</c>,
/// <c>BadNodeIdUnknown</c>, ...) is a refused read, not an empty one, so it must reach the caller
/// instead of collapsing to "0 values returned". <c>BadNoData</c> is the one Bad code that genuinely
/// means "the window held nothing" and stays an empty result.
/// </summary>
internal static class HistoryReadStatus
{
/// <summary>Throws a <see cref="ServiceResultException"/> naming the node and status when the read was refused.</summary>
/// <param name="status">The per-node result status.</param>
/// <param name="nodeId">The node that was read, for the message.</param>
/// <exception cref="ServiceResultException"><paramref name="status"/> is Bad and not <c>BadNoData</c>.</exception>
public static void ThrowIfRefused(StatusCode status, NodeId nodeId)
{
if (!StatusCode.IsBad(status) || status.Code == StatusCodes.BadNoData) return;
throw new ServiceResultException(status.Code,
$"HistoryRead of {nodeId} failed: {StatusCodes.GetBrowseName(status.Code)} (0x{status.Code:X8}).");
}
}