Skip to content

Commit

Permalink
Improve err handling
Browse files Browse the repository at this point in the history
  • Loading branch information
luiscantero committed Jul 29, 2024
1 parent 178ddf6 commit 8c1d9c5
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/PlcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ public override ResponseHeader CreateMonitoredItems(
out MonitoredItemCreateResultCollection results,
out DiagnosticInfoCollection diagnosticInfos)
{
results = default;
diagnosticInfos = default;

try
{
OperationContext context = ValidateRequest(requestHeader, RequestType.CreateMonitoredItems);
Expand All @@ -180,9 +183,43 @@ public override ResponseHeader CreateMonitoredItems(

return responseHeader;
}
catch (ServiceResultException ex) when (ex.StatusCode == StatusCodes.BadNoSubscription)
{
MetricsHelper.RecordTotalErrors(nameof(CreateMonitoredItems));

_logger.LogDebug(
ex,
"Failed creating monitored items: {StatusCode}",
StatusCodes.BadNoSubscription.ToString());

return new ResponseHeader { ServiceResult = StatusCodes.BadNoSubscription };
}
catch (ServiceResultException ex) when (ex.StatusCode == StatusCodes.BadSessionIdInvalid)
{
MetricsHelper.RecordTotalErrors(nameof(CreateMonitoredItems));

_logger.LogDebug(
ex,
"Failed creating monitored items: {StatusCode}",
StatusCodes.BadSessionIdInvalid.ToString());

return new ResponseHeader { ServiceResult = StatusCodes.BadSessionIdInvalid };
}
catch (ServiceResultException ex) when (ex.StatusCode == StatusCodes.BadSecureChannelIdInvalid)
{
MetricsHelper.RecordTotalErrors(nameof(CreateMonitoredItems));

_logger.LogDebug(
ex,
"Failed creating monitored items: {StatusCode}",
StatusCodes.BadSecureChannelIdInvalid.ToString());

return new ResponseHeader { ServiceResult = StatusCodes.BadSecureChannelIdInvalid };
}
catch (Exception ex)
{
MetricsHelper.RecordTotalErrors(nameof(CreateMonitoredItems));

_logger.LogError(ex, "Error creating monitored items");
throw;
}
Expand Down Expand Up @@ -289,6 +326,9 @@ public override ResponseHeader Read(
out DataValueCollection results,
out DiagnosticInfoCollection diagnosticInfos)
{
results = default;
diagnosticInfos = default;

try
{
var responseHeader = base.Read(requestHeader, maxAge, timestampsToReturn, nodesToRead, out results, out diagnosticInfos);
Expand All @@ -297,9 +337,43 @@ public override ResponseHeader Read(

return responseHeader;
}
catch (ServiceResultException ex) when (ex.StatusCode == StatusCodes.BadNoSubscription)
{
MetricsHelper.RecordTotalErrors(nameof(Read));

_logger.LogDebug(
ex,
"Failed to read: {StatusCode}",
StatusCodes.BadNoSubscription.ToString());

return new ResponseHeader { ServiceResult = StatusCodes.BadNoSubscription };
}
catch (ServiceResultException ex) when (ex.StatusCode == StatusCodes.BadSessionIdInvalid)
{
MetricsHelper.RecordTotalErrors(nameof(Read));

_logger.LogDebug(
ex,
"Failed to read: {StatusCode}",
StatusCodes.BadSessionIdInvalid.ToString());

return new ResponseHeader { ServiceResult = StatusCodes.BadSessionIdInvalid };
}
catch (ServiceResultException ex) when (ex.StatusCode == StatusCodes.BadSecureChannelIdInvalid)
{
MetricsHelper.RecordTotalErrors(nameof(Read));

_logger.LogDebug(
ex,
"Failed to read: {StatusCode}",
StatusCodes.BadSecureChannelIdInvalid.ToString());

return new ResponseHeader { ServiceResult = StatusCodes.BadSecureChannelIdInvalid };
}
catch (Exception ex)
{
MetricsHelper.RecordTotalErrors(nameof(Read));

_logger.LogError(ex, "Error reading");
throw;
}
Expand Down

0 comments on commit 8c1d9c5

Please sign in to comment.