From 9cf003481c1bc0b497abd36de7bf995b5f2be2ce Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 3 Apr 2023 15:57:31 +0100 Subject: [PATCH] refactor: move exception messages to resource file (#27) --- X10D.Tests/src/Linq/EnumerableTests.cs | 2 +- X10D/src/Collections/BoolListExtensions.cs | 8 +-- X10D/src/Collections/CollectionExtensions.cs | 4 +- X10D/src/Core/SpanExtensions.cs | 4 +- X10D/src/Drawing/Circle.cs | 2 +- X10D/src/Drawing/CircleF.cs | 2 +- X10D/src/Drawing/Line.cs | 2 +- X10D/src/Drawing/Line3D.cs | 2 +- X10D/src/Drawing/LineF.cs | 2 +- X10D/src/Drawing/Sphere.cs | 2 +- X10D/src/ExceptionMessages.Designer.cs | 54 ++++++++++++++++++++ X10D/src/ExceptionMessages.resx | 18 +++++++ X10D/src/Linq/EnumerableExtensions.cs | 12 ++--- X10D/src/Math/DecimalExtensions.cs | 2 +- 14 files changed, 94 insertions(+), 22 deletions(-) diff --git a/X10D.Tests/src/Linq/EnumerableTests.cs b/X10D.Tests/src/Linq/EnumerableTests.cs index 336b21d..5318d4b 100644 --- a/X10D.Tests/src/Linq/EnumerableTests.cs +++ b/X10D.Tests/src/Linq/EnumerableTests.cs @@ -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); } } } diff --git a/X10D/src/Collections/BoolListExtensions.cs b/X10D/src/Collections/BoolListExtensions.cs index ee70050..347d19b 100644 --- a/X10D/src/Collections/BoolListExtensions.cs +++ b/X10D/src/Collections/BoolListExtensions.cs @@ -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; diff --git a/X10D/src/Collections/CollectionExtensions.cs b/X10D/src/Collections/CollectionExtensions.cs index 152e7ba..3554586 100644 --- a/X10D/src/Collections/CollectionExtensions.cs +++ b/X10D/src/Collections/CollectionExtensions.cs @@ -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) diff --git a/X10D/src/Core/SpanExtensions.cs b/X10D/src/Core/SpanExtensions.cs index d46ae5f..5447cb5 100644 --- a/X10D/src/Core/SpanExtensions.cs +++ b/X10D/src/Core/SpanExtensions.cs @@ -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()} bytes is unexpected."); + throw new UnreachableException(ExceptionMessages.EnumSizeIsUnexpected); #else - throw new ArgumentException($"Enum with the size of {Unsafe.SizeOf()} bytes is unexpected."); + throw new ArgumentException(ExceptionMessages.EnumSizeIsUnexpected); #endif //NOSONAR // dotcover enable diff --git a/X10D/src/Drawing/Circle.cs b/X10D/src/Drawing/Circle.cs index 1ba36c6..75e3f23 100644 --- a/X10D/src/Drawing/Circle.cs +++ b/X10D/src/Drawing/Circle.cs @@ -230,7 +230,7 @@ public readonly struct Circle : IEquatable, IComparable, ICompar if (obj is not Circle other) { - throw new ArgumentException($"Object must be of type {GetType()}"); + throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType); } return CompareTo(other); diff --git a/X10D/src/Drawing/CircleF.cs b/X10D/src/Drawing/CircleF.cs index 6a64962..7832eb3 100644 --- a/X10D/src/Drawing/CircleF.cs +++ b/X10D/src/Drawing/CircleF.cs @@ -241,7 +241,7 @@ public readonly struct CircleF : IEquatable, IComparable, ICom if (obj is not CircleF other) { - throw new ArgumentException($"Object must be of type {GetType()}"); + throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType); } return CompareTo(other); diff --git a/X10D/src/Drawing/Line.cs b/X10D/src/Drawing/Line.cs index c174b9f..4c24060 100644 --- a/X10D/src/Drawing/Line.cs +++ b/X10D/src/Drawing/Line.cs @@ -245,7 +245,7 @@ public readonly struct Line : IEquatable, IComparable, IComparable if (obj is not Line other) { - throw new ArgumentException($"Object must be of type {GetType()}"); + throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType); } return CompareTo(other); diff --git a/X10D/src/Drawing/Line3D.cs b/X10D/src/Drawing/Line3D.cs index f2b3bc5..d2e4177 100644 --- a/X10D/src/Drawing/Line3D.cs +++ b/X10D/src/Drawing/Line3D.cs @@ -250,7 +250,7 @@ public readonly struct Line3D : IEquatable, IComparable, ICompar if (obj is not Line3D other) { - throw new ArgumentException($"Object must be of type {GetType()}"); + throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType); } return CompareTo(other); diff --git a/X10D/src/Drawing/LineF.cs b/X10D/src/Drawing/LineF.cs index 8e1b172..c4b31b2 100644 --- a/X10D/src/Drawing/LineF.cs +++ b/X10D/src/Drawing/LineF.cs @@ -254,7 +254,7 @@ public readonly struct LineF : IEquatable, IComparable, IComparabl if (obj is not LineF other) { - throw new ArgumentException($"Object must be of type {GetType()}"); + throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType); } return CompareTo(other); diff --git a/X10D/src/Drawing/Sphere.cs b/X10D/src/Drawing/Sphere.cs index 9d557c4..26d91d6 100644 --- a/X10D/src/Drawing/Sphere.cs +++ b/X10D/src/Drawing/Sphere.cs @@ -210,7 +210,7 @@ public readonly struct Sphere : IEquatable, IComparable, ICompar if (obj is not Sphere other) { - throw new ArgumentException($"Object must be of type {GetType()}"); + throw new ArgumentException(ExceptionMessages.ObjectIsNotAValidType); } return CompareTo(other); diff --git a/X10D/src/ExceptionMessages.Designer.cs b/X10D/src/ExceptionMessages.Designer.cs index da0bc9f..ebabf39 100644 --- a/X10D/src/ExceptionMessages.Designer.cs +++ b/X10D/src/ExceptionMessages.Designer.cs @@ -68,6 +68,24 @@ namespace X10D { } } + /// + /// Looks up a localized string similar to Collection is read-only. Try using DisposeAll instead.. + /// + internal static string CollectionIsReadOnly_DisposeAll { + get { + return ResourceManager.GetString("CollectionIsReadOnly_DisposeAll", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Collection is read-only. Try using DisposeAllAsync instead.. + /// + internal static string CollectionIsReadOnly_DisposeAllAsync { + get { + return ResourceManager.GetString("CollectionIsReadOnly_DisposeAllAsync", resourceCulture); + } + } + /// /// Looks up a localized string similar to count must be greater than or equal to 0.. /// @@ -131,6 +149,15 @@ namespace X10D { } } + /// + /// Looks up a localized string similar to The enum has a size that is not supported.. + /// + internal static string EnumSizeIsUnexpected { + get { + return ResourceManager.GetString("EnumSizeIsUnexpected", resourceCulture); + } + } + /// /// Looks up a localized string similar to HashAlgorithm's Create method returned null reference.. /// @@ -194,6 +221,24 @@ namespace X10D { } } + /// + /// Looks up a localized string similar to The specified object is not a valid type.. + /// + internal static string ObjectIsNotAValidType { + get { + return ResourceManager.GetString("ObjectIsNotAValidType", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The source contains no elements.. + /// + internal static string SourceContainsNoElements { + get { + return ResourceManager.GetString("SourceContainsNoElements", resourceCulture); + } + } + /// /// Looks up a localized string similar to The source contains too many elements.. /// @@ -266,6 +311,15 @@ namespace X10D { } } + /// + /// Looks up a localized string similar to Value cannot be negative.. + /// + internal static string ValueCannotBeNegative { + get { + return ResourceManager.GetString("ValueCannotBeNegative", resourceCulture); + } + } + /// /// Looks up a localized string similar to Year cannot be zero.. /// diff --git a/X10D/src/ExceptionMessages.resx b/X10D/src/ExceptionMessages.resx index 556a96f..9bc8b28 100644 --- a/X10D/src/ExceptionMessages.resx +++ b/X10D/src/ExceptionMessages.resx @@ -123,6 +123,12 @@ Count must be positive and count must refer to a location within the string/array/collection. + + Collection is read-only. Try using DisposeAll instead. + + + Collection is read-only. Try using DisposeAllAsync instead. + The destination span is too short to contain the data. @@ -138,6 +144,12 @@ Type provided must be an Enum. + + The enum has a size that is not supported. + + + The specified object is not a valid type. + {0} is not a class. @@ -159,6 +171,9 @@ Length must be greater than or equal to 0. + + The source contains no elements. + The source contains too many elements. @@ -189,4 +204,7 @@ Rune.Utf8SequenceLength returns value {0} which is outside range 1 to 4 (inclusive), which is unexpected according to the official documentation. + + Value cannot be negative. + \ No newline at end of file diff --git a/X10D/src/Linq/EnumerableExtensions.cs b/X10D/src/Linq/EnumerableExtensions.cs index 178d03a..4362dd2 100644 --- a/X10D/src/Linq/EnumerableExtensions.cs +++ b/X10D/src/Linq/EnumerableExtensions.cs @@ -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]); diff --git a/X10D/src/Math/DecimalExtensions.cs b/X10D/src/Math/DecimalExtensions.cs index 94ab81b..280c81d 100644 --- a/X10D/src/Math/DecimalExtensions.cs +++ b/X10D/src/Math/DecimalExtensions.cs @@ -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;