forked from S7NetPlus/s7netplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request S7NetPlus#196 from mycroes/invalid-data-on-open
Invalid data on open
- Loading branch information
Showing
8 changed files
with
78 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters