refactor: move exception messages to resource file (#27)

This commit is contained in:
Oliver Booth 2023-04-03 15:57:31 +01:00
parent 3c85ae6f64
commit 9cf003481c
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
14 changed files with 94 additions and 22 deletions

View File

@ -233,7 +233,7 @@ public class EnumerableTests
return obj is Person other
? CompareTo(other)
: throw new ArgumentException($"Object must be of type {nameof(Person)}");
: throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType);
}
}
}

View File

@ -29,7 +29,7 @@ public static class BoolListExtensions
if (source.Count > 8)
{
throw new ArgumentException("Source cannot contain more than than 8 elements.", nameof(source));
throw new ArgumentException(ExceptionMessages.SourceSpanIsTooLarge, nameof(source));
}
byte result = 0;
@ -63,7 +63,7 @@ public static class BoolListExtensions
if (source.Count > 16)
{
throw new ArgumentException("Source cannot contain more than than 16 elements.", nameof(source));
throw new ArgumentException(ExceptionMessages.SourceSpanIsTooLarge, nameof(source));
}
short result = 0;
@ -97,7 +97,7 @@ public static class BoolListExtensions
if (source.Count > 32)
{
throw new ArgumentException("Source cannot contain more than than 32 elements.", nameof(source));
throw new ArgumentException(ExceptionMessages.SourceSpanIsTooLarge, nameof(source));
}
var result = 0;
@ -131,7 +131,7 @@ public static class BoolListExtensions
if (source.Count > 64)
{
throw new ArgumentException("Source cannot contain more than than 64 elements.", nameof(source));
throw new ArgumentException(ExceptionMessages.SourceSpanIsTooLarge, nameof(source));
}
var result = 0L;

View File

@ -27,7 +27,7 @@ public static class CollectionExtensions
if (source.IsReadOnly)
{
throw new InvalidOperationException("Collection is read-only. Try using DisposeAll instead.");
throw new InvalidOperationException(ExceptionMessages.CollectionIsReadOnly_DisposeAll);
}
foreach (T item in source)
@ -66,7 +66,7 @@ public static class CollectionExtensions
if (source.IsReadOnly)
{
throw new InvalidOperationException("Collection is read-only. Try using DisposeAllAsync instead.");
throw new InvalidOperationException(ExceptionMessages.CollectionIsReadOnly_DisposeAllAsync);
}
foreach (T item in source)

View File

@ -113,9 +113,9 @@ public static class SpanExtensions
//NOSONAR
default:
#if NET7_0_OR_GREATER
throw new UnreachableException($"Enum with the size of {Unsafe.SizeOf<T>()} bytes is unexpected.");
throw new UnreachableException(ExceptionMessages.EnumSizeIsUnexpected);
#else
throw new ArgumentException($"Enum with the size of {Unsafe.SizeOf<T>()} bytes is unexpected.");
throw new ArgumentException(ExceptionMessages.EnumSizeIsUnexpected);
#endif
//NOSONAR
// dotcover enable

View File

@ -230,7 +230,7 @@ public readonly struct Circle : IEquatable<Circle>, IComparable<Circle>, ICompar
if (obj is not Circle other)
{
throw new ArgumentException($"Object must be of type {GetType()}");
throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType);
}
return CompareTo(other);

View File

@ -241,7 +241,7 @@ public readonly struct CircleF : IEquatable<CircleF>, IComparable<CircleF>, ICom
if (obj is not CircleF other)
{
throw new ArgumentException($"Object must be of type {GetType()}");
throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType);
}
return CompareTo(other);

View File

@ -245,7 +245,7 @@ public readonly struct Line : IEquatable<Line>, IComparable<Line>, IComparable
if (obj is not Line other)
{
throw new ArgumentException($"Object must be of type {GetType()}");
throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType);
}
return CompareTo(other);

View File

@ -250,7 +250,7 @@ public readonly struct Line3D : IEquatable<Line3D>, IComparable<Line3D>, ICompar
if (obj is not Line3D other)
{
throw new ArgumentException($"Object must be of type {GetType()}");
throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType);
}
return CompareTo(other);

View File

@ -254,7 +254,7 @@ public readonly struct LineF : IEquatable<LineF>, IComparable<LineF>, IComparabl
if (obj is not LineF other)
{
throw new ArgumentException($"Object must be of type {GetType()}");
throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType);
}
return CompareTo(other);

View File

@ -210,7 +210,7 @@ public readonly struct Sphere : IEquatable<Sphere>, IComparable<Sphere>, ICompar
if (obj is not Sphere other)
{
throw new ArgumentException($"Object must be of type {GetType()}");
throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType);
}
return CompareTo(other);

View File

@ -68,6 +68,24 @@ namespace X10D {
}
}
/// <summary>
/// Looks up a localized string similar to Collection is read-only. Try using DisposeAll instead..
/// </summary>
internal static string CollectionIsReadOnly_DisposeAll {
get {
return ResourceManager.GetString("CollectionIsReadOnly_DisposeAll", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Collection is read-only. Try using DisposeAllAsync instead..
/// </summary>
internal static string CollectionIsReadOnly_DisposeAllAsync {
get {
return ResourceManager.GetString("CollectionIsReadOnly_DisposeAllAsync", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to count must be greater than or equal to 0..
/// </summary>
@ -131,6 +149,15 @@ namespace X10D {
}
}
/// <summary>
/// Looks up a localized string similar to The enum has a size that is not supported..
/// </summary>
internal static string EnumSizeIsUnexpected {
get {
return ResourceManager.GetString("EnumSizeIsUnexpected", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to HashAlgorithm&apos;s Create method returned null reference..
/// </summary>
@ -194,6 +221,24 @@ namespace X10D {
}
}
/// <summary>
/// Looks up a localized string similar to The specified object is not a valid type..
/// </summary>
internal static string ObjectIsNotAValidType {
get {
return ResourceManager.GetString("ObjectIsNotAValidType", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The source contains no elements..
/// </summary>
internal static string SourceContainsNoElements {
get {
return ResourceManager.GetString("SourceContainsNoElements", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The source contains too many elements..
/// </summary>
@ -266,6 +311,15 @@ namespace X10D {
}
}
/// <summary>
/// Looks up a localized string similar to Value cannot be negative..
/// </summary>
internal static string ValueCannotBeNegative {
get {
return ResourceManager.GetString("ValueCannotBeNegative", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Year cannot be zero..
/// </summary>

View File

@ -123,6 +123,12 @@
<data name="CountMustBeInRange" xml:space="preserve">
<value>Count must be positive and count must refer to a location within the string/array/collection.</value>
</data>
<data name="CollectionIsReadOnly_DisposeAll" xml:space="preserve">
<value>Collection is read-only. Try using DisposeAll instead.</value>
</data>
<data name="CollectionIsReadOnly_DisposeAllAsync" xml:space="preserve">
<value>Collection is read-only. Try using DisposeAllAsync instead.</value>
</data>
<data name="DestinationSpanLengthTooShort" xml:space="preserve">
<value>The destination span is too short to contain the data.</value>
</data>
@ -138,6 +144,12 @@
<data name="EnumParseNotEnumException" xml:space="preserve">
<value>Type provided must be an Enum.</value>
</data>
<data name="EnumSizeIsUnexpected" xml:space="preserve">
<value>The enum has a size that is not supported.</value>
</data>
<data name="ObjectIsNotAValidType" xml:space="preserve">
<value>The specified object is not a valid type.</value>
</data>
<data name="TypeIsNotClass" xml:space="preserve">
<value>{0} is not a class.</value>
</data>
@ -159,6 +171,9 @@
<data name="LengthGreaterThanOrEqualTo0" xml:space="preserve">
<value>Length must be greater than or equal to 0.</value>
</data>
<data name="SourceContainsNoElements" xml:space="preserve">
<value>The source contains no elements.</value>
</data>
<data name="SourceSpanIsTooLarge" xml:space="preserve">
<value>The source contains too many elements.</value>
</data>
@ -189,4 +204,7 @@
<data name="UnexpectedRuneUtf8SequenceLength" xml:space="preserve">
<value>Rune.Utf8SequenceLength returns value {0} which is outside range 1 to 4 (inclusive), which is unexpected according to the official documentation.</value>
</data>
<data name="ValueCannotBeNegative" xml:space="preserve">
<value>Value cannot be negative.</value>
</data>
</root>

View File

@ -97,7 +97,7 @@ public static class EnumerableExtensions
{
if (!enumerator.MoveNext())
{
throw new InvalidOperationException("Source contains no elements");
throw new InvalidOperationException(ExceptionMessages.SourceContainsNoElements);
}
minValue = enumerator.Current;
@ -200,7 +200,7 @@ public static class EnumerableExtensions
{
if (!enumerator.MoveNext())
{
throw new InvalidOperationException("Source contains no elements");
throw new InvalidOperationException(ExceptionMessages.SourceContainsNoElements);
}
minValue = selector(enumerator.Current);
@ -301,7 +301,7 @@ public static class EnumerableExtensions
{
if (!enumerator.MoveNext())
{
throw new InvalidOperationException("Source contains no elements");
throw new InvalidOperationException(ExceptionMessages.SourceContainsNoElements);
}
minValue = enumerator.Current;
@ -331,7 +331,7 @@ public static class EnumerableExtensions
{
if (span.IsEmpty)
{
throw new InvalidOperationException("Source contains no elements");
throw new InvalidOperationException(ExceptionMessages.SourceContainsNoElements);
}
T minValue = span[0];
@ -361,7 +361,7 @@ public static class EnumerableExtensions
{
if (span.IsEmpty)
{
throw new InvalidOperationException("Source contains no elements");
throw new InvalidOperationException(ExceptionMessages.SourceContainsNoElements);
}
TSource minValue = span[0];
@ -392,7 +392,7 @@ public static class EnumerableExtensions
{
if (span.IsEmpty)
{
throw new InvalidOperationException("Source contains no elements");
throw new InvalidOperationException(ExceptionMessages.SourceContainsNoElements);
}
TResult minValue = selector(span[0]);

View File

@ -192,7 +192,7 @@ public static class DecimalExtensions
case 0:
return 0;
case < 0:
throw new ArgumentException("value cannot be negative", nameof(value));
throw new ArgumentException(ExceptionMessages.ValueCannotBeNegative, nameof(value));
}
decimal previous;