You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Improve StringExtensions exception messages and add comprehensive unit tests
- Enhanced exception messages with detailed input information and actionable guidance
- Added proper parameter names for ArgumentException and ArgumentNullException
- Included input data display with appropriate truncation for debugging
- Improved error messages for UTF-8 encoding/decoding operations
- Enhanced hex conversion error reporting with clear validation guidance
- Added comprehensive unit tests covering all exception scenarios
- Verified backward compatibility (null hex strings still return empty arrays)
- All 78 tests in Neo.Extensions.Tests now pass successfully
Exception messages now include:
1. Specific parameter names for better debugging
2. Input data information with smart truncation
3. Actionable guidance for resolution
4. Consistent formatting across the codebase
Fixes improve developer experience and debugging efficiency.
* Update src/Neo.Extensions/StringExtensions.cs
Co-authored-by: Christopher Schuchardt <[email protected]>
---------
Co-authored-by: Will <[email protected]>
Co-authored-by: Shargon <[email protected]>
Co-authored-by: Christopher Schuchardt <[email protected]>
thrownewDecoderFallbackException($"Failed to decode byte span to UTF-8 string (strict mode): The input contains invalid UTF-8 byte sequences. {bytesInfo}. Ensure all bytes form valid UTF-8 character sequences.",ex);
70
+
}
71
+
catch(ArgumentExceptionex)
72
+
{
73
+
thrownewArgumentException("Invalid byte span provided for UTF-8 decoding. The span may be corrupted or contain invalid data.",nameof(value),ex);
74
+
}
75
+
catch(Exceptionex)
76
+
{
77
+
thrownewInvalidOperationException("An unexpected error occurred while decoding byte span to UTF-8 string in strict mode. This may indicate a system-level encoding issue.",ex);
78
+
}
79
+
}
60
80
61
81
/// <summary>
62
82
/// Converts a byte array to a strict UTF8 string.
63
83
/// </summary>
64
84
/// <param name="value">The byte array to convert.</param>
thrownewArgumentNullException(nameof(value),"Cannot decode null byte array to UTF-8 string.");
91
+
92
+
try
93
+
{
94
+
returnStrictUTF8.GetString(value);
95
+
}
96
+
catch(DecoderFallbackExceptionex)
97
+
{
98
+
varbytesInfo=value.Length<=32?$"Bytes: {BitConverter.ToString(value)}":$"Length: {value.Length} bytes, First 16: {BitConverter.ToString(value,0,Math.Min(16,value.Length))}...";
99
+
thrownewDecoderFallbackException($"Failed to decode byte array to UTF-8 string (strict mode): The input contains invalid UTF-8 byte sequences. {bytesInfo}. Ensure all bytes form valid UTF-8 character sequences.",ex);
100
+
}
101
+
catch(ArgumentExceptionex)
102
+
{
103
+
thrownewArgumentException("Invalid byte array provided for UTF-8 decoding. The array may be corrupted or contain invalid data.",nameof(value),ex);
104
+
}
105
+
catch(Exceptionex)
106
+
{
107
+
thrownewInvalidOperationException("An unexpected error occurred while decoding byte array to UTF-8 string in strict mode. This may indicate a system-level encoding issue.",ex);
108
+
}
109
+
}
68
110
69
111
/// <summary>
70
112
/// Converts a byte array to a strict UTF8 string.
thrownewArgumentNullException(nameof(value),"Cannot decode null byte array to UTF-8 string.");
123
+
if(start<0)
124
+
thrownewArgumentOutOfRangeException(nameof(start),start,"Start index cannot be negative.");
125
+
if(count<0)
126
+
thrownewArgumentOutOfRangeException(nameof(count),count,"Count cannot be negative.");
127
+
if(start+count>value.Length)
128
+
thrownewArgumentOutOfRangeException(nameof(count),$"The specified range [{start}, {start+count}) exceeds the array bounds (length: {value.Length}). Ensure start + count <= array.Length.");
129
+
130
+
try
131
+
{
132
+
returnStrictUTF8.GetString(value,start,count);
133
+
}
134
+
catch(DecoderFallbackExceptionex)
135
+
{
136
+
varrangeBytes=newbyte[count];
137
+
Array.Copy(value,start,rangeBytes,0,count);
138
+
varbytesInfo=count<=32?$"Bytes: {BitConverter.ToString(rangeBytes)}":$"Length: {count} bytes, First 16: {BitConverter.ToString(rangeBytes,0,Math.Min(16,count))}...";
139
+
thrownewDecoderFallbackException($"Failed to decode byte array range [{start}, {start+count}) to UTF-8 string (strict mode): The input contains invalid UTF-8 byte sequences. {bytesInfo}. Ensure all bytes form valid UTF-8 character sequences.",ex);
thrownewInvalidOperationException($"An unexpected error occurred while decoding byte array range [{start}, {start+count}) to UTF-8 string in strict mode. This may indicate a system-level encoding issue.",ex);
148
+
}
149
+
}
79
150
80
151
/// <summary>
81
152
/// Converts a string to a strict UTF8 byte array.
82
153
/// </summary>
83
154
/// <param name="value">The string to convert.</param>
thrownewArgumentNullException(nameof(value),"Cannot encode null string to UTF-8 bytes.");
161
+
162
+
try
163
+
{
164
+
returnStrictUTF8.GetBytes(value);
165
+
}
166
+
catch(EncoderFallbackExceptionex)
167
+
{
168
+
varvalueInfo=value.Length<=100?$"Input: '{value}'":$"Input length: {value.Length} characters, First 50: '{value[..50]}...'";
169
+
thrownewEncoderFallbackException($"Failed to encode string to UTF-8 bytes (strict mode): The input contains characters that cannot be encoded in UTF-8. {valueInfo}. Ensure the string contains only valid Unicode characters.",ex);
170
+
}
171
+
catch(ArgumentExceptionex)
172
+
{
173
+
thrownewArgumentException("Invalid string provided for UTF-8 encoding. The string may contain unsupported characters.",nameof(value),ex);
174
+
}
175
+
catch(Exceptionex)
176
+
{
177
+
thrownewInvalidOperationException("An unexpected error occurred while encoding string to UTF-8 bytes in strict mode. This may indicate a system-level encoding issue.",ex);
178
+
}
179
+
}
87
180
88
181
/// <summary>
89
182
/// Gets the size of the specified <see cref="string"/> encoded in strict UTF8.
thrownewArgumentNullException(nameof(value),"Cannot get UTF-8 byte count for null string.");
191
+
192
+
try
193
+
{
194
+
returnStrictUTF8.GetByteCount(value);
195
+
}
196
+
catch(EncoderFallbackExceptionex)
197
+
{
198
+
varvalueInfo=value.Length<=100?$"Input: '{value}'":$"Input length: {value.Length} characters, First 50: '{value[..50]}...'";
199
+
thrownewEncoderFallbackException($"Failed to get UTF-8 byte count for string (strict mode): The input contains characters that cannot be encoded in UTF-8. {valueInfo}. Ensure the string contains only valid Unicode characters.",ex);
200
+
}
201
+
catch(ArgumentExceptionex)
202
+
{
203
+
thrownewArgumentException("Invalid string provided for UTF-8 byte count calculation. The string may contain unsupported characters.",nameof(value),ex);
204
+
}
205
+
catch(Exceptionex)
206
+
{
207
+
thrownewInvalidOperationException("An unexpected error occurred while calculating UTF-8 byte count for string in strict mode. This may indicate a system-level encoding issue.",ex);
208
+
}
209
+
}
95
210
96
211
/// <summary>
97
212
/// Determines if the specified <see cref="string"/> is a valid hex string.
@@ -119,7 +234,31 @@ public static bool IsHex(this string value)
119
234
/// <param name="value">The hex <see cref="string"/> to convert.</param>
thrownewArgumentException($"Failed to convert hex string to bytes: The input has an invalid length (must be even) or contains non-hexadecimal characters. {valueInfo}. Valid hex characters are 0-9, A-F, and a-f.",nameof(value),ex);
thrownewFormatException($"Failed to convert hex string to bytes: The input contains invalid hexadecimal characters. {valueInfo}. Valid hex characters are 0-9, A-F, and a-f.",ex);
thrownewInvalidOperationException($"An unexpected error occurred while converting hex string to bytes. {valueInfo}. This may indicate a system-level parsing issue.",ex);
260
+
}
261
+
}
123
262
124
263
/// <summary>
125
264
/// Converts a hex <see cref="string"/> to byte array then reverses the order of the bytes.
@@ -129,9 +268,27 @@ public static bool IsHex(this string value)
thrownewArgumentException($"Failed to convert hex span to reversed bytes: The input has an invalid length (must be even) or contains non-hexadecimal characters. {valueInfo}. Valid hex characters are 0-9, A-F, and a-f.",ex);
thrownewFormatException($"Failed to convert hex span to reversed bytes: The input contains invalid hexadecimal characters. {valueInfo}. Valid hex characters are 0-9, A-F, and a-f.",ex);
thrownewInvalidOperationException($"An unexpected error occurred while converting hex span to reversed bytes. {valueInfo}. This may indicate a system-level parsing or array manipulation issue.",ex);
291
+
}
135
292
}
136
293
137
294
/// <summary>
@@ -141,7 +298,25 @@ public static byte[] HexToBytesReversed(this ReadOnlySpan<char> value)
thrownewArgumentException($"Failed to convert hex span to bytes: The input has an invalid length (must be even) or contains non-hexadecimal characters. {valueInfo}. Valid hex characters are 0-9, A-F, and a-f.",ex);
thrownewFormatException($"Failed to convert hex span to bytes: The input contains invalid hexadecimal characters. {valueInfo}. Valid hex characters are 0-9, A-F, and a-f.",ex);
thrownewInvalidOperationException($"An unexpected error occurred while converting hex span to bytes. {valueInfo}. This may indicate a system-level parsing issue.",ex);
319
+
}
145
320
}
146
321
147
322
/// <summary>
@@ -151,8 +326,24 @@ public static byte[] HexToBytes(this ReadOnlySpan<char> value)
151
326
/// <returns>The size of the <see cref="string"/>.</returns>
152
327
publicstaticintGetVarSize(thisstringvalue)
153
328
{
154
-
varsize=value.GetStrictUtf8ByteCount();
155
-
returnsize.GetVarSize()+size;
329
+
if(value==null)
330
+
thrownewArgumentNullException(nameof(value),"Cannot calculate variable size for null string.");
331
+
332
+
try
333
+
{
334
+
varsize=value.GetStrictUtf8ByteCount();
335
+
returnsize.GetVarSize()+size;
336
+
}
337
+
catch(EncoderFallbackExceptionex)
338
+
{
339
+
varvalueInfo=value.Length<=100?$"Input: '{value}'":$"Input length: {value.Length} characters, First 50: '{value[..50]}...'";
340
+
thrownewEncoderFallbackException($"Failed to calculate variable size: The string contains characters that cannot be encoded in UTF-8 (strict mode). {valueInfo}. Ensure the string contains only valid Unicode characters.",ex);
thrownewInvalidOperationException($"An unexpected error occurred while calculating variable size for string. {valueInfo}. This may indicate an issue with the string encoding or variable size calculation.",ex);
0 commit comments