From 20458d4b4636defc3e26ddbf66fcf31f90c1ea28 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Wed, 10 Oct 2018 21:41:13 +0200 Subject: [PATCH] Add InvalidDataException --- S7.Net/InvalidDataException.cs | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 S7.Net/InvalidDataException.cs diff --git a/S7.Net/InvalidDataException.cs b/S7.Net/InvalidDataException.cs new file mode 100644 index 0000000..560d764 --- /dev/null +++ b/S7.Net/InvalidDataException.cs @@ -0,0 +1,43 @@ +using System; + +namespace S7.Net +{ + #if NET_FULL + [Serializable] + #endif + public class InvalidDataException : Exception + { + public byte[] ReceivedData { get; } + public int ErrorIndex { get; } + public byte ExpectedValue { get; } + + public InvalidDataException(string message, byte[] receivedData, int errorIndex, byte expectedValue) + : base(FormatMessage(message, receivedData, errorIndex, expectedValue)) + { + ReceivedData = receivedData; + ErrorIndex = errorIndex; + ExpectedValue = expectedValue; + } + + #if NET_FULL + protected InvalidDataException(System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) + { + ReceivedData = (byte[]) info.GetValue(nameof(ReceivedData), typeof(byte[])); + ErrorIndex = info.GetInt32(nameof(ErrorIndex)); + ExpectedValue = info.GetByte(nameof(ExpectedValue)); + } + #endif + + private static string FormatMessage(string message, byte[] receivedData, int errorIndex, byte expectedValue) + { + if (errorIndex >= receivedData.Length) + throw new ArgumentOutOfRangeException(nameof(errorIndex), + $"{nameof(errorIndex)} {errorIndex} is outside the bounds of {nameof(receivedData)} with length {receivedData.Length}."); + + return $"{message} Invalid data received. Expected '{expectedValue}' at index {errorIndex}, " + + $"but received {receivedData[errorIndex]}. See the {nameof(ReceivedData)} property " + + "for the full message received."; + } + } +} \ No newline at end of file