Skip to content

Commit e527ed7

Browse files
author
Petr Sramek
committed
moved exception initialization and throw to factory method
1 parent 575d699 commit e527ed7

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

src/PolylineAlgorithm/InvalidCoordinateException.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ namespace PolylineAlgorithm;
1313
/// </summary>
1414
[SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "Main purpose is to report coordinate that is invalid, thus we have to have only one construtor.")]
1515
public sealed class InvalidCoordinateException : Exception {
16-
public InvalidCoordinateException(Coordinate coordinate, Exception? innerException = null)
17-
: base(string.Format(ExceptionMessageResource.CoordinateIsOutOfRangeMessageFormat, coordinate.Latitude, coordinate.Longitude), innerException) {
16+
private InvalidCoordinateException(Coordinate coordinate, string message, Exception? innerException = null)
17+
: base(message, innerException) {
1818
Coordinate = coordinate;
1919
}
2020

2121
/// <summary>
2222
/// Coordinate that caused the exception.
2323
/// </summary>
2424
public Coordinate Coordinate { get; }
25-
}
25+
26+
public static void Throw(Coordinate coordinate, Exception? innerException = null) {
27+
throw new InvalidCoordinateException(coordinate, string.Format(ExceptionMessageResource.CoordinateIsOutOfRangeMessageFormat, coordinate.Latitude, coordinate.Longitude), innerException);
28+
}
29+
}

src/PolylineAlgorithm/InvalidReaderStateException.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ namespace PolylineAlgorithm;
1313
/// Represents error that is caused by invalid reader state.
1414
/// </summary>
1515
[SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "Main purpose is to report position in which failure occurs, thus we have to have only one constructor.")]
16-
public class InvalidReaderStateException : Exception {
17-
public InvalidReaderStateException(int position, int length)
18-
: base(GetErrorMessage(position, length)) {
19-
}
16+
public sealed class InvalidReaderStateException(string message)
17+
: Exception(message) {
2018

21-
private static string GetErrorMessage(int readerPosition, int polylineLength) {
19+
public static void Throw(int readerPosition, int polylineLength) {
2220
if (polylineLength == 0) {
23-
return ExceptionMessageResource.PolylineStringIsEmptyMessage;
21+
throw new InvalidReaderStateException(ExceptionMessageResource.PolylineStringIsEmptyMessage);
2422
} else {
25-
return string.Format(ExceptionMessageResource.UnableToReadPolylineAtPositionMessageFormat, readerPosition, polylineLength);
23+
throw new InvalidReaderStateException(string.Format(ExceptionMessageResource.UnableToReadPolylineAtPositionMessageFormat, readerPosition, polylineLength));
2624
}
2725
}
2826
}

src/PolylineAlgorithm/InvalidWriterStateException.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ namespace PolylineAlgorithm;
1313
/// Represents error that is caused by invalid reader state.
1414
/// </summary>
1515
[SuppressMessage("Design", "CA1032:Implement standard exception constructors", Justification = "Main purpose is to report position in which failure occurs, thus we have to have only one constructor.")]
16-
public class InvalidWriterStateException : Exception {
17-
public InvalidWriterStateException(int position, int bufferSize)
18-
: base(GetErrorMessage(position, bufferSize)) {
19-
}
16+
public sealed class InvalidWriterStateException(string message)
17+
: Exception(message) {
2018

21-
private static string GetErrorMessage(int readerPosition, int bufferSize) {
22-
if (bufferSize == 0) {
23-
return ExceptionMessageResource.PolylineBufferIsEmptyMessage;
19+
public static void Throw(int readerPosition, int polylineLength) {
20+
if (polylineLength == 0) {
21+
throw new InvalidWriterStateException(ExceptionMessageResource.PolylineBufferIsEmptyMessage);
2422
} else {
25-
return string.Format(ExceptionMessageResource.UnableToWritePolylineBufferAtPositionMessageFormat, readerPosition, bufferSize);
23+
throw new InvalidWriterStateException(string.Format(ExceptionMessageResource.UnableToWritePolylineBufferAtPositionMessageFormat, readerPosition, bufferSize));
2624
}
2725
}
2826
}

src/PolylineAlgorithm/PolylineMalformedException.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ public sealed class PolylineMalformedException : Exception {
1919
/// </summary>
2020
/// <param name="position"></param>
2121
/// <param name="innerException"></param>
22-
public PolylineMalformedException(int position, Exception? innerException = null)
23-
: base(string.Format(ExceptionMessageResource.PolylineStringIsMalformedMessage, position),
24-
innerException) {
22+
private PolylineMalformedException(int position, string message, Exception? innerException = null)
23+
: base(message, innerException) {
2524
Position = position;
2625
}
2726

2827
/// <summary>
2928
/// Position in polyline string at which error occurs.
3029
/// </summary>
3130
public int Position { get; }
31+
32+
public static void Throw(int position, Exception? innerException = null) {
33+
throw new PolylineMalformedException(position, string.Format(ExceptionMessageResource.PolylineStringIsMalformedMessage, position), innerException);
34+
}
3235
}

0 commit comments

Comments
 (0)