Skip to content

Commit 356311e

Browse files
committed
refactored exception messages and messages formats, removed unused
1 parent cc81c27 commit 356311e

File tree

9 files changed

+42
-55
lines changed

9 files changed

+42
-55
lines changed

src/PolylineAlgorithm/Coordinate.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace PolylineAlgorithm;
77

8+
using PolylineAlgorithm.Properties;
89
using System;
910
using System.Diagnostics;
1011
using System.Globalization;
@@ -44,11 +45,11 @@ public Coordinate() {
4445
/// </exception>
4546
public Coordinate(double latitude, double longitude) {
4647
if (latitude < -90 || latitude > 90 || double.IsNaN(latitude) || double.IsInfinity(latitude)) {
47-
throw new ArgumentOutOfRangeException(nameof(latitude), "Latitude must be between -90 and 90.");
48+
throw new ArgumentOutOfRangeException(nameof(latitude), string.Format(ExceptionMessageResource.CoordinateValueMustBeBetweenValuesMessageFormat, "Latitude", -90, 90));
4849
}
4950

5051
if (longitude < -180 || longitude > 180 || double.IsNaN(longitude) || double.IsInfinity(longitude)) {
51-
throw new ArgumentOutOfRangeException(nameof(longitude), "Longitude must be between -180 and 180.");
52+
throw new ArgumentOutOfRangeException(nameof(longitude), string.Format(ExceptionMessageResource.CoordinateValueMustBeBetweenValuesMessageFormat, "Longitude", -180, 180));
5253
}
5354

5455
Latitude = latitude;

src/PolylineAlgorithm/InvalidPolylineException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private InvalidPolylineException(string message)
3737
/// <exception cref="InvalidPolylineException">
3838
/// Always thrown to indicate that the polyline is malformed at the specified position.
3939
/// </exception>
40-
public static void Throw(long position) {
40+
internal static void Throw(long position) {
4141
Debug.Assert(position >= 0, "Position must be a non-negative value.");
4242

4343
throw new InvalidPolylineException(string.Format(ExceptionMessageResource.PolylineStringIsMalformedMessage, position.ToString()));

src/PolylineAlgorithm/Polyline.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace PolylineAlgorithm;
77

8+
using PolylineAlgorithm.Properties;
89
using System;
910
using System.Diagnostics;
1011
using System.Runtime.InteropServices;
@@ -70,8 +71,8 @@ public void CopyTo(char[] destination) {
7071
throw new ArgumentNullException(nameof(destination));
7172
}
7273

73-
if (Length != destination.Length) {
74-
throw new ArgumentException("Destination array length must match the polyline's length.", nameof(destination));
74+
if (destination.Length < Length) {
75+
throw new ArgumentException(ExceptionMessageResource.DestinationArrayLengthMustBeEqualOrGreaterThanPolylineLengthMessage, nameof(destination));
7576
}
7677

7778
_value.CopyTo(destination);

src/PolylineAlgorithm/PolylineEncoding.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ public static bool TryReadValue(ref int variance, ref ReadOnlyMemory<char> buffe
9292
public static double Denormalize(int value, CoordinateValueType type) {
9393
// Validate that the type is not None, as it does not represent a valid coordinate value type.
9494
if (type == CoordinateValueType.None) {
95-
throw new ArgumentOutOfRangeException(nameof(type), string.Format(ExceptionMessageResource.ArgumentCannotBeCoordinateCoordinateValueTypeErrorFormat, type.ToString()));
95+
throw new ArgumentOutOfRangeException(nameof(type), string.Format(ExceptionMessageResource.ArgumentCannotBeCoordinateValueTypeMessageFormat, type.ToString()));
9696
}
9797

9898
// Validate that the value is finite and within the acceptable range for the specified type.
9999
if (!ValidateValue(value, type)) {
100-
throw new ArgumentOutOfRangeException(nameof(value), value, string.Format(ExceptionMessageResource.ArgumentIsOutOfRangeForSpecifiedType, type.ToString().ToLowerInvariant()));
100+
throw new ArgumentOutOfRangeException(nameof(value), value, string.Format(ExceptionMessageResource.ArgumentOutOfRangeForSpecifiedCoordinateValueTypeMessageFormat, type.ToString().ToLowerInvariant()));
101101
}
102102

103103
// Return fast if the value is zero, return 0.0 as the denormalized value.
@@ -181,7 +181,7 @@ public static bool TryWriteValue(int variance, ref Span<char> buffer, ref int po
181181
public static int Normalize(double value, CoordinateValueType type) {
182182
// Validate that the type is not None, as it does not represent a valid coordinate value type.
183183
if (type == CoordinateValueType.None) {
184-
throw new ArgumentOutOfRangeException(nameof(type), string.Format(ExceptionMessageResource.ArgumentCannotBeCoordinateCoordinateValueTypeErrorFormat, type.ToString()));
184+
throw new ArgumentOutOfRangeException(nameof(type), string.Format(ExceptionMessageResource.ArgumentCannotBeCoordinateValueTypeMessageFormat, type.ToString()));
185185
}
186186

187187
// Validate that the value is finite and not NaN or Infinity.
@@ -191,7 +191,7 @@ public static int Normalize(double value, CoordinateValueType type) {
191191

192192
// Validate that the value is within the acceptable range for the specified type.
193193
if (!ValidateValue(value, type)) {
194-
throw new ArgumentOutOfRangeException(nameof(value), value, string.Format(ExceptionMessageResource.ArgumentIsOutOfRangeForSpecifiedType, type.ToString().ToLowerInvariant()));
194+
throw new ArgumentOutOfRangeException(nameof(value), value, string.Format(ExceptionMessageResource.ArgumentOutOfRangeForSpecifiedCoordinateValueTypeMessageFormat, type.ToString().ToLowerInvariant()));
195195
}
196196

197197
// Fast return if the value is zero, return 0 as the normalized value.

src/PolylineAlgorithm/PolylineEncodingOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace PolylineAlgorithm;
1515
/// <remarks>
1616
/// This class allows you to set options such as buffer size and logger factory for encoding operations.
1717
/// </remarks>
18-
[DebuggerDisplay("BufferSize: {BufferSize}, MaxBufferLength: {MaxBufferLength}, LoggerFactoryType: {LoggerFactory.GetType().Name}")]
18+
[DebuggerDisplay("BufferSizeInBytes: {BufferSizeInBytes}, MaxBufferLength: {MaxBufferLength}, LoggerFactoryType: {LoggerFactory.GetType().Name}")]
1919
public sealed class PolylineEncodingOptions {
2020
/// <summary>
2121
/// Gets the maximum buffer size for encoding operations.

src/PolylineAlgorithm/PolylineEncodingOptionsBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace PolylineAlgorithm;
77

88
using Microsoft.Extensions.Logging;
99
using Microsoft.Extensions.Logging.Abstractions;
10+
using PolylineAlgorithm.Properties;
1011

1112
/// <summary>
1213
/// Provides a builder for configuring options for polyline encoding operations.
@@ -51,7 +52,7 @@ public PolylineEncodingOptions Build() {
5152
/// </returns>
5253
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="maxBufferSize"/> is less than or equal to 11.</exception>
5354
public PolylineEncodingOptionsBuilder WithBufferSize(int maxBufferSize) {
54-
_bufferSize = maxBufferSize > 11 ? maxBufferSize : throw new ArgumentOutOfRangeException(nameof(maxBufferSize), "Buffer size must be greater than 11.");
55+
_bufferSize = maxBufferSize > 11 ? maxBufferSize : throw new ArgumentOutOfRangeException(nameof(maxBufferSize), string.Format(ExceptionMessageResource.BufferSizeMustBeGreaterThanMessageFormat, 11));
5556

5657
return this;
5758
}
@@ -69,7 +70,7 @@ public PolylineEncodingOptionsBuilder WithBufferSize(int maxBufferSize) {
6970
/// Thrown when <paramref name="loggerFactory"/> is <see langword="null"/>.
7071
/// </exception>
7172
public PolylineEncodingOptionsBuilder WithLoggerFactory(ILoggerFactory loggerFactory) {
72-
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory), "Logger factory cannot be null.");
73+
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
7374

7475
return this;
7576
}

src/PolylineAlgorithm/Properties/ExceptionMessageResource.Designer.cs

Lines changed: 16 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PolylineAlgorithm/Properties/ExceptionMessageResource.resx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,34 +117,31 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120-
<data name="CoordinatesAreInvalidErrorMessage" xml:space="preserve">
121-
<value>One or more coordinates are invalid. Check InnerExceptions property for details.</value>
122-
</data>
123120
<data name="ArgumentCannotBeEmptyEnumerationMessage" xml:space="preserve">
124121
<value>Argument cannot be an empty enumeration.</value>
125122
</data>
126-
<data name="CoordinateIsOutOfRangeMessageFormat" xml:space="preserve">
127-
<value>Latitude {0} or longitude {1} is not valid. Latitude must be in range between -90 and +90. Longitude must be in range between -180 and +180.</value>
128-
</data>
129123
<data name="PolylineStringIsMalformedMessage" xml:space="preserve">
130124
<value>Polyline is malformed at position {0}.</value>
131125
</data>
132-
<data name="ArgumentMinCannotBeGreaterOfEqualThanMaxArgumentMessageFormat" xml:space="preserve">
133-
<value>Argument {0} cannot be greater or equal than {1}.</value>
134-
</data>
135126
<data name="PolylineCannotBeShorterThanExceptionMessage" xml:space="preserve">
136127
<value>Argument cannot be shorten than 2 characters. Actual length: {0}.</value>
137128
</data>
138129
<data name="ArgumentValueMustBeFiniteNumber" xml:space="preserve">
139130
<value>Value must be a finite number.</value>
140131
</data>
141-
<data name="ArgumentIsOutOfRangeForSpecifiedType" xml:space="preserve">
132+
<data name="ArgumentOutOfRangeForSpecifiedCoordinateValueTypeMessageFormat" xml:space="preserve">
142133
<value>Value is out of range for the specified type {0}.</value>
143134
</data>
144-
<data name="ArgumentCannotBeNullEmptyOrWhiteSpaceMessage" xml:space="preserve">
145-
<value>Argument cannot be null -or- empty -or- white space.</value>
146-
</data>
147-
<data name="ArgumentCannotBeCoordinateCoordinateValueTypeErrorFormat" xml:space="preserve">
135+
<data name="ArgumentCannotBeCoordinateValueTypeMessageFormat" xml:space="preserve">
148136
<value>Argument cannot be CoordinateValueType.{0}.</value>
149137
</data>
138+
<data name="BufferSizeMustBeGreaterThanMessageFormat" xml:space="preserve">
139+
<value>Buffer size must be greater than {0}.</value>
140+
</data>
141+
<data name="DestinationArrayLengthMustBeEqualOrGreaterThanPolylineLengthMessage" xml:space="preserve">
142+
<value>Destination array length must be greater than the polyline length.</value>
143+
</data>
144+
<data name="CoordinateValueMustBeBetweenValuesMessageFormat" xml:space="preserve">
145+
<value>{0} must be between {1} and {2}.</value>
146+
</data>
150147
</root>

tests/PolylineAlgorithm.Tests/AbstractPolylineDecoderTest.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,6 @@ protected override (double Latitude, double Longitude) CreateCoordinate(double l
164164
}
165165

166166
protected override ReadOnlyMemory<char> GetReadOnlyMemory(string? polyline) {
167-
if (string.IsNullOrWhiteSpace(polyline)) {
168-
throw new ArgumentException(ExceptionMessageResource.ArgumentCannotBeNullEmptyOrWhiteSpaceMessage, nameof(polyline));
169-
}
170-
171167
return polyline.AsMemory();
172168
}
173169
}

0 commit comments

Comments
 (0)