diff --git a/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs b/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs
index 687635c..e093224 100644
--- a/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs
+++ b/src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs
@@ -12,7 +12,7 @@ public static partial class GuardClauseExtensions
///
///
/// Optional. Custom error message
- /// Optional. Custom exception
+ /// Optional. Custom exception
/// if the value is not an empty string.
///
///
@@ -21,10 +21,12 @@ public static ReadOnlySpan Empty(this IGuardClause guardClause,
ReadOnlySpan input,
string parameterName,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
if (input.Length == 0 || input == string.Empty)
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
}
return input;
@@ -37,17 +39,19 @@ public static ReadOnlySpan Empty(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- /// Optional. Custom exception
+ /// Optional. Custom exception
/// if the value is not an empty or whitespace string.
///
public static ReadOnlySpan WhiteSpace(this IGuardClause guardClause,
ReadOnlySpan input,
string parameterName,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
if (MemoryExtensions.IsWhiteSpace(input))
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName!);
}
diff --git a/src/GuardClauses/GuardAgainstExpressionExtensions.cs b/src/GuardClauses/GuardAgainstExpressionExtensions.cs
index 5b3ad67..7b770d3 100644
--- a/src/GuardClauses/GuardAgainstExpressionExtensions.cs
+++ b/src/GuardClauses/GuardAgainstExpressionExtensions.cs
@@ -17,7 +17,7 @@ public static partial class GuardClauseExtensions
/// The input to evaluate.
/// The message to include in the exception if the input is invalid.
/// The name of the parameter to include in the thrown exception, captured automatically from the input expression.
- ///
+ ///
/// The if the evaluates to false, indicating a valid state.
/// Thrown when the validation function returns true, indicating that the input is invalid.
///
@@ -26,11 +26,13 @@ public static T Expression(this IGuardClause guardClause,
T input,
string message,
[CallerArgumentExpression("input")] string? parameterName = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
where T : struct
{
if (func(input))
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message, parameterName!);
}
@@ -48,7 +50,7 @@ public static T Expression(this IGuardClause guardClause,
/// The input to evaluate.
/// The message to include in the exception if the input is invalid.
/// The name of the parameter to include in the thrown exception, captured automatically from the input expression.
- ///
+ ///
/// if the evaluates to true
/// Thrown when the validation function returns true, indicating that the input is invalid.
///
@@ -57,11 +59,13 @@ public static async Task ExpressionAsync(this IGuardClause guardClause,
T input,
string message,
[CallerArgumentExpression("input")] string? parameterName = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
where T : struct
{
if (await func(input))
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message, parameterName!);
}
diff --git a/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs b/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs
index 2d65862..28bfd44 100644
--- a/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs
+++ b/src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs
@@ -14,7 +14,7 @@ public static partial class GuardClauseExtensions
///
///
/// Optional. Custom error message
- ///
+ ///
///
///
///
@@ -23,11 +23,13 @@ public static string InvalidFormat(this IGuardClause guardClause,
string parameterName,
string regexPattern,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
var m = Regex.Match(input, regexPattern);
if (!m.Success || input != m.Value)
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message ?? $"Input {parameterName} was not in required format", parameterName);
}
@@ -42,7 +44,7 @@ public static string InvalidFormat(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
///
///
///
@@ -51,10 +53,12 @@ public static T InvalidInput(this IGuardClause guardClause,
T input, string parameterName,
Func predicate,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
if (!predicate(input))
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message ?? $"Input {parameterName} did not satisfy the options", parameterName);
}
@@ -69,7 +73,7 @@ public static T InvalidInput(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
///
///
///
@@ -79,10 +83,12 @@ public static async Task InvalidInputAsync(this IGuardClause guardClause,
string parameterName,
Func> predicate,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
if (!await predicate(input))
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message ?? $"Input {parameterName} did not satisfy the options", parameterName);
}
diff --git a/src/GuardClauses/GuardAgainstNegativeExtensions.cs b/src/GuardClauses/GuardAgainstNegativeExtensions.cs
index a724557..7086fbd 100644
--- a/src/GuardClauses/GuardAgainstNegativeExtensions.cs
+++ b/src/GuardClauses/GuardAgainstNegativeExtensions.cs
@@ -12,7 +12,7 @@ public static partial class GuardClauseExtensions
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative.
///
///
@@ -20,9 +20,9 @@ public static int Negative(this IGuardClause guardClause,
int input,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- return Negative(guardClause, input, parameterName, message, exception);
+ return Negative(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -32,7 +32,7 @@ public static int Negative(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative.
///
///
@@ -40,9 +40,9 @@ public static long Negative(this IGuardClause guardClause,
long input,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- return Negative(guardClause, input, parameterName, message, exception);
+ return Negative(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -52,16 +52,16 @@ public static long Negative(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative.
///
public static decimal Negative(this IGuardClause guardClause,
decimal input,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- return Negative(guardClause, input, parameterName, message, exception);
+ return Negative(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -71,15 +71,15 @@ public static decimal Negative(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative.
///
public static float Negative(this IGuardClause guardClause,
float input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
- return Negative(guardClause, input, parameterName, message, exception);
+ return Negative(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -89,15 +89,15 @@ public static float Negative(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative.
///
public static double Negative(this IGuardClause guardClause,
double input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
- return Negative(guardClause, input, parameterName, message, exception);
+ return Negative(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -107,15 +107,15 @@ public static double Negative(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative.
///
public static TimeSpan Negative(this IGuardClause guardClause,
TimeSpan input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
- return Negative(guardClause, input, parameterName, message, exception);
+ return Negative(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -125,17 +125,19 @@ public static TimeSpan Negative(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative.
///
///
private static T Negative(this IGuardClause guardClause,
T input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null) where T : struct, IComparable
+ string? message = null, Func? exceptionCreator = null) where T : struct, IComparable
{
if (input.CompareTo(default(T)) < 0)
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} cannot be negative.", parameterName!);
}
@@ -149,16 +151,16 @@ private static T Negative(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative or zero.
///
///
public static int NegativeOrZero(this IGuardClause guardClause,
int input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
- return NegativeOrZero(guardClause, input, parameterName, message, exception);
+ return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -168,16 +170,16 @@ public static int NegativeOrZero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative or zero.
///
///
public static long NegativeOrZero(this IGuardClause guardClause,
long input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
- return NegativeOrZero(guardClause, input, parameterName, message, exception);
+ return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -187,16 +189,16 @@ public static long NegativeOrZero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative or zero.
///
///
public static decimal NegativeOrZero(this IGuardClause guardClause,
decimal input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
- return NegativeOrZero(guardClause, input, parameterName, message, exception);
+ return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -206,16 +208,16 @@ public static decimal NegativeOrZero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative or zero.
///
///
public static float NegativeOrZero(this IGuardClause guardClause,
float input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
- return NegativeOrZero(guardClause, input, parameterName, message, exception);
+ return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -225,16 +227,16 @@ public static float NegativeOrZero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative or zero.
///
///
public static double NegativeOrZero(this IGuardClause guardClause,
double input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
- return NegativeOrZero(guardClause, input, parameterName, message, exception);
+ return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -244,16 +246,16 @@ public static double NegativeOrZero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative or zero.
///
///
public static TimeSpan NegativeOrZero(this IGuardClause guardClause,
TimeSpan input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
- return NegativeOrZero(guardClause, input, parameterName, message, exception);
+ return NegativeOrZero(guardClause, input, parameterName, message, exceptionCreator);
}
///
@@ -264,7 +266,7 @@ public static TimeSpan NegativeOrZero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative or zero.
///
///
@@ -272,10 +274,12 @@ private static T NegativeOrZero(this IGuardClause guardClause,
T input,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null) where T : struct, IComparable
+ Func? exceptionCreator = null) where T : struct, IComparable
{
if (input.CompareTo(default(T)) <= 0)
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} cannot be zero or negative.", parameterName!);
}
diff --git a/src/GuardClauses/GuardAgainstNotFoundExtensions.cs b/src/GuardClauses/GuardAgainstNotFoundExtensions.cs
index ea3aeae..bdd38d9 100644
--- a/src/GuardClauses/GuardAgainstNotFoundExtensions.cs
+++ b/src/GuardClauses/GuardAgainstNotFoundExtensions.cs
@@ -14,7 +14,7 @@ public static partial class GuardClauseExtensions
///
///
///
- ///
+ ///
/// if the value is not null.
///
///
@@ -22,12 +22,14 @@ public static T NotFound(this IGuardClause guardClause,
[NotNull][ValidatedNotNull] string key,
[NotNull][ValidatedNotNull] T? input,
[CallerArgumentExpression("input")] string? parameterName = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
guardClause.NullOrEmpty(key, nameof(key));
if (input is null)
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new NotFoundException(key, parameterName!);
}
@@ -43,7 +45,7 @@ public static T NotFound(this IGuardClause guardClause,
///
///
///
- ///
+ ///
/// if the value is not null.
///
///
@@ -51,12 +53,14 @@ public static T NotFound(this IGuardClause guardClause,
[NotNull][ValidatedNotNull] TKey key,
[NotNull][ValidatedNotNull]T? input,
[CallerArgumentExpression("input")] string? parameterName = null,
- Exception? exception = null) where TKey : struct
+ Func? exceptionCreator = null) where TKey : struct
{
guardClause.Null(key, nameof(key));
if (input is null)
{
+ Exception? exception = exceptionCreator?.Invoke();
+
// TODO: Can we safely consider that ToString() won't return null for struct?
throw exception ?? new NotFoundException(key.ToString()!, parameterName!);
}
diff --git a/src/GuardClauses/GuardAgainstNullExtensions.cs b/src/GuardClauses/GuardAgainstNullExtensions.cs
index 431a33b..0fda06f 100644
--- a/src/GuardClauses/GuardAgainstNullExtensions.cs
+++ b/src/GuardClauses/GuardAgainstNullExtensions.cs
@@ -248,6 +248,6 @@ public static T NullOrInvalidInput(this IGuardClause guardClause,
{
Guard.Against.Null(input, parameterName, message, exceptionCreator: exceptionCreator);
- return Guard.Against.InvalidInput(input, parameterName, predicate, message, exceptionCreator?.Invoke());
+ return Guard.Against.InvalidInput(input, parameterName, predicate, message, exceptionCreator);
}
}
diff --git a/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs b/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs
index 4b52faf..ef74293 100644
--- a/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs
+++ b/src/GuardClauses/GuardAgainstOutOfRangeExtensions.cs
@@ -31,9 +31,9 @@ public static string LengthOutOfRange(this IGuardClause guardClause,
Func? exceptionCreator = null)
{
Guard.Against.Negative(maxLength - minLength, parameterName: "min or max length",
- message: "Min length must be equal or less than max length.", exception: exceptionCreator?.Invoke());
- Guard.Against.StringTooShort(input, minLength, nameof(minLength), exception: exceptionCreator?.Invoke());
- Guard.Against.StringTooLong(input, maxLength, nameof(maxLength), exception: exceptionCreator?.Invoke());
+ message: "Min length must be equal or less than max length.", exceptionCreator: exceptionCreator);
+ Guard.Against.StringTooShort(input, minLength, nameof(minLength), exceptionCreator: exceptionCreator);
+ Guard.Against.StringTooLong(input, maxLength, nameof(maxLength), exceptionCreator: exceptionCreator);
return input;
}
@@ -149,7 +149,7 @@ public static IEnumerable OutOfRange(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is in the range of valid SqlDateTime values.
///
///
@@ -157,10 +157,10 @@ public static IEnumerable OutOfRange(this IGuardClause guardClause,
public static DateTime NullOrOutOfSQLDateRange(this IGuardClause guardClause,
[NotNull][ValidatedNotNull] DateTime? input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
- guardClause.Null(input, nameof(input), exceptionCreator: () =>exception);
- return OutOfSQLDateRange(guardClause, input.Value, parameterName, message, exception);
+ guardClause.Null(input, nameof(input), exceptionCreator: exceptionCreator);
+ return OutOfSQLDateRange(guardClause, input.Value, parameterName, message, exceptionCreator);
}
///
@@ -170,20 +170,20 @@ public static DateTime NullOrOutOfSQLDateRange(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is in the range of valid SqlDateTime values.
///
///
public static DateTime OutOfSQLDateRange(this IGuardClause guardClause,
DateTime input,
[CallerArgumentExpression("input")] string? parameterName = null,
- string? message = null, Exception? exception = null)
+ string? message = null, Func? exceptionCreator = null)
{
// System.Data is unavailable in .NET Standard so we can't use SqlDateTime.
const long sqlMinDateTicks = 552877920000000000;
const long sqlMaxDateTicks = 3155378975999970000;
- return NullOrOutOfRangeInternal(guardClause, input, parameterName, new DateTime(sqlMinDateTicks), new DateTime(sqlMaxDateTicks), message,exception);
+ return NullOrOutOfRangeInternal(guardClause, input, parameterName, new DateTime(sqlMinDateTicks), new DateTime(sqlMaxDateTicks), message, exceptionCreator);
}
///
@@ -195,7 +195,7 @@ public static DateTime OutOfSQLDateRange(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not out of range.
///
///
@@ -204,9 +204,9 @@ public static T OutOfRange(this IGuardClause guardClause,
string parameterName,
[NotNull][ValidatedNotNull] T rangeFrom,
[NotNull][ValidatedNotNull] T rangeTo,
- string? message = null, Exception? exception = null) where T : IComparable, IComparable
+ string? message = null, Func? exceptionCreator = null) where T : IComparable, IComparable
{
- return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message,exception);
+ return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message, exceptionCreator);
}
@@ -220,7 +220,7 @@ public static T OutOfRange(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not not null or out of range.
///
///
@@ -231,10 +231,10 @@ public static T NullOrOutOfRange(this IGuardClause guardClause,
string parameterName,
[NotNull][ValidatedNotNull] T rangeFrom,
[NotNull][ValidatedNotNull] T rangeTo,
- string? message = null, Exception? exception = null) where T : IComparable
+ string? message = null, Func? exceptionCreator = null) where T : IComparable
{
- guardClause.Null(input, nameof(input),exceptionCreator: () => exception);
- return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message, exception);
+ guardClause.Null(input, nameof(input),exceptionCreator: exceptionCreator);
+ return NullOrOutOfRangeInternal(guardClause, input, parameterName, rangeFrom, rangeTo, message, exceptionCreator);
}
///
@@ -247,7 +247,7 @@ public static T NullOrOutOfRange(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not not null or out of range.
///
///
@@ -258,10 +258,10 @@ public static T NullOrOutOfRange(this IGuardClause guardClause,
string parameterName,
[NotNull][ValidatedNotNull] T rangeFrom,
[NotNull][ValidatedNotNull] T rangeTo,
- string? message = null, Exception? exception = null) where T : struct, IComparable
+ string? message = null, Func? exceptionCreator = null) where T : struct, IComparable
{
- guardClause.Null(input, nameof(input), exceptionCreator: () => exception);
- return NullOrOutOfRangeInternal(guardClause, input.Value, parameterName, rangeFrom, rangeTo, message, exception);
+ guardClause.Null(input, nameof(input), exceptionCreator: exceptionCreator);
+ return NullOrOutOfRangeInternal(guardClause, input.Value, parameterName, rangeFrom, rangeTo, message, exceptionCreator);
}
///
@@ -273,7 +273,7 @@ private static T NullOrOutOfRangeInternal(this IGuardClause guardClause,
[NotNull][ValidatedNotNull] T? rangeFrom,
[NotNull][ValidatedNotNull] T? rangeTo,
string? message = null,
- Exception? exception = null) where T : IComparable?
+ Func? exceptionCreator = null) where T : IComparable?
{
Guard.Against.Null(input, nameof(input));
Guard.Against.Null(parameterName, nameof(parameterName));
@@ -287,6 +287,8 @@ private static T NullOrOutOfRangeInternal(this IGuardClause guardClause,
if (input.CompareTo(rangeFrom) < 0 || input.CompareTo(rangeTo) > 0)
{
+ Exception? exception = exceptionCreator?.Invoke();
+
if (string.IsNullOrEmpty(message))
{
throw exception ?? new ArgumentOutOfRangeException(parameterName, $"Input {parameterName} was out of range");
diff --git a/src/GuardClauses/GuardAgainstStringLengthExtensions.cs b/src/GuardClauses/GuardAgainstStringLengthExtensions.cs
index b85cab0..e31bbbc 100644
--- a/src/GuardClauses/GuardAgainstStringLengthExtensions.cs
+++ b/src/GuardClauses/GuardAgainstStringLengthExtensions.cs
@@ -17,7 +17,7 @@ public static partial class GuardClauseExtensions
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative.
///
///
@@ -26,11 +26,13 @@ public static string StringTooShort(this IGuardClause guardClause,
int minLength,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- Guard.Against.NegativeOrZero(minLength, nameof(minLength), exception: exception);
+ Guard.Against.NegativeOrZero(minLength, nameof(minLength), exceptionCreator: exceptionCreator);
if (input.Length < minLength)
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too short. Minimum length is {minLength}.", parameterName);
}
return input;
@@ -44,7 +46,7 @@ public static string StringTooShort(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not negative.
///
///
@@ -53,11 +55,13 @@ public static string StringTooLong(this IGuardClause guardClause,
int maxLength,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- Guard.Against.NegativeOrZero(maxLength, nameof(maxLength), exception: exception);
+ Guard.Against.NegativeOrZero(maxLength, nameof(maxLength), exceptionCreator: exceptionCreator);
if (input.Length > maxLength)
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message ?? $"Input {parameterName} with length {input.Length} is too long. Maximum length is {maxLength}.", parameterName);
}
return input;
diff --git a/src/GuardClauses/GuardAgainstZeroExtensions.cs b/src/GuardClauses/GuardAgainstZeroExtensions.cs
index 9f204f5..bcc0bea 100644
--- a/src/GuardClauses/GuardAgainstZeroExtensions.cs
+++ b/src/GuardClauses/GuardAgainstZeroExtensions.cs
@@ -13,7 +13,7 @@ public static partial class GuardClauseExtensions
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not zero.
///
///
@@ -21,9 +21,9 @@ public static int Zero(this IGuardClause guardClause,
int input,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- return Zero(guardClause, input, parameterName!, message, exception);
+ return Zero(guardClause, input, parameterName!, message, exceptionCreator);
}
///
@@ -33,7 +33,7 @@ public static int Zero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not zero.
///
///
@@ -41,9 +41,9 @@ public static long Zero(this IGuardClause guardClause,
long input,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- return Zero(guardClause, input, parameterName!, message, exception);
+ return Zero(guardClause, input, parameterName!, message, exceptionCreator);
}
///
@@ -53,7 +53,7 @@ public static long Zero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not zero.
///
///
@@ -61,9 +61,9 @@ public static decimal Zero(this IGuardClause guardClause,
decimal input,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- return Zero(guardClause, input, parameterName!, message,exception);
+ return Zero(guardClause, input, parameterName!, message,exceptionCreator);
}
///
@@ -73,7 +73,7 @@ public static decimal Zero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not zero.
///
///
@@ -81,9 +81,9 @@ public static float Zero(this IGuardClause guardClause,
float input,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- return Zero(guardClause, input, parameterName!, message, exception);
+ return Zero(guardClause, input, parameterName!, message, exceptionCreator);
}
///
@@ -93,7 +93,7 @@ public static float Zero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not zero.
///
///
@@ -101,9 +101,9 @@ public static double Zero(this IGuardClause guardClause,
double input,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- return Zero(guardClause, input, parameterName!, message,exception);
+ return Zero(guardClause, input, parameterName!, message, exceptionCreator);
}
///
@@ -112,16 +112,16 @@ public static double Zero(this IGuardClause guardClause,
///
///
///
- ///
+ ///
/// if the value is not zero.
///
///
public static TimeSpan Zero(this IGuardClause guardClause,
TimeSpan input,
[CallerArgumentExpression("input")] string? parameterName = null,
- Exception? exception = null)
+ Func? exceptionCreator = null)
{
- return Zero(guardClause, input, parameterName!, exception:exception);
+ return Zero(guardClause, input, parameterName!, exceptionCreator: exceptionCreator);
}
///
@@ -131,15 +131,17 @@ public static TimeSpan Zero(this IGuardClause guardClause,
///
///
/// Optional. Custom error message
- ///
+ ///
/// if the value is not zero.
///
///
private static T Zero(this IGuardClause guardClause, T input, string parameterName, string? message = null,
- Exception? exception = null) where T : struct
+ Func? exceptionCreator = null) where T : struct
{
if (EqualityComparer.Default.Equals(input, default(T)))
{
+ Exception? exception = exceptionCreator?.Invoke();
+
throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} cannot be zero.", parameterName);
}
diff --git a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs
index 0d84018..4c9c51c 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstExpression.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstExpression.cs
@@ -34,7 +34,7 @@ public void GivenIntegerWhenTheExpressionEvaluatesToTrueThrowsCustomExceptionWhe
{
Exception customException = new Exception();
int testCase = 10;
- Assert.Throws(() => Guard.Against.Expression((x) => x == 10, testCase, "Value cannot be 10", exception: customException));
+ Assert.Throws(() => Guard.Against.Expression((x) => x == 10, testCase, "Value cannot be 10", exceptionCreator: () => customException));
}
@@ -57,7 +57,7 @@ public void GivenDoubleWhenTheExpressionEvaluatesToTrueThrowsCustomExceptionWhen
{
Exception customException = new Exception();
double testCase = 1.1;
- Assert.Throws(() => Guard.Against.Expression((x) => x == 1.1, testCase, "Value cannot be 1.1", exception: customException));
+ Assert.Throws(() => Guard.Against.Expression((x) => x == 1.1, testCase, "Value cannot be 1.1", exceptionCreator: () => customException));
}
@@ -80,7 +80,7 @@ public void GivenCustomStructWhenTheExpressionEvaluatesToTrueThrowsException(Cus
public void GivenCustomStructWhenTheExpressionEvaluatesToTrueThrowsCustomExceptionWhenSupplied(CustomStruct test)
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.Expression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching", exception: customException));
+ Assert.Throws(() => Guard.Against.Expression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching", exceptionCreator: () => customException));
}
diff --git a/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs b/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs
index f4f7358..b20002e 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs
@@ -40,7 +40,7 @@ public void ThrowsGivenGivenIncorrectFormat(string input, string regexPattern)
public void ThrowsCustomExceptionWhenSuppliedGivenGivenIncorrectFormat(string input, string regexPattern)
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.InvalidFormat(input, nameof(input), regexPattern, exception: customException));
+ Assert.Throws(() => Guard.Against.InvalidFormat(input, nameof(input), regexPattern, exceptionCreator: () => customException));
}
diff --git a/test/GuardClauses.UnitTests/GuardAgainstNegative.cs b/test/GuardClauses.UnitTests/GuardAgainstNegative.cs
index c711050..bb2a09c 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstNegative.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstNegative.cs
@@ -39,7 +39,7 @@ public void ThrowsGivenNegativeIntValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeIntValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.Negative(-1, "negative", exception: customException));
+ Assert.Throws(() => Guard.Against.Negative(-1, "negative", exceptionCreator: () => customException));
}
@@ -53,7 +53,7 @@ public void ThrowsGivenNegativeLongValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeLongValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.Negative(-1L, "negative", exception: customException));
+ Assert.Throws(() => Guard.Against.Negative(-1L, "negative", exceptionCreator: () => customException));
}
@@ -67,7 +67,7 @@ public void ThrowsGivenNegativeDecimalValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeDecimalValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.Negative(-1.0M, "negative", exception: customException));
+ Assert.Throws(() => Guard.Against.Negative(-1.0M, "negative", exceptionCreator: () => customException));
}
@@ -81,7 +81,7 @@ public void ThrowsGivenNegativeFloatValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeFloatValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.Negative(-1.0f, "negative", exception: customException));
+ Assert.Throws(() => Guard.Against.Negative(-1.0f, "negative", exceptionCreator: () => customException));
}
@@ -95,7 +95,7 @@ public void ThrowsGivenNegativeDoubleValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeDoubleValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.Negative(-1.0, "negative", exception: customException));
+ Assert.Throws(() => Guard.Against.Negative(-1.0, "negative", exceptionCreator: () => customException));
}
@@ -109,7 +109,7 @@ public void ThrowsGivenNegativeTimeSpanValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeTimeSpanValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.Negative(TimeSpan.FromSeconds(-1), "negative", exception: customException));
+ Assert.Throws(() => Guard.Against.Negative(TimeSpan.FromSeconds(-1), "negative", exceptionCreator: () => customException));
}
diff --git a/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs b/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs
index f566a1b..cd28825 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs
@@ -28,7 +28,7 @@ public void ThrowsGivenZeroIntValue()
public void ThrowsCustomExceptionWhenSuppliedGivenZeroIntValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(0, "intZero", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(0, "intZero", exceptionCreator: () => customException));
}
@@ -42,7 +42,7 @@ public void ThrowsGivenZeroLongValue()
public void ThrowsCustomExceptionWhenSuppliedGivenZeroLongValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(0L, "longZero", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(0L, "longZero", exceptionCreator: () => customException));
}
@@ -56,7 +56,7 @@ public void ThrowsGivenZeroDecimalValue()
public void ThrowsCustomExceptionWhenSuppliedGivenZeroDecimalValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(0M, "decimalZero", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(0M, "decimalZero", exceptionCreator: () => customException));
}
@@ -70,7 +70,7 @@ public void ThrowsGivenZeroFloatValue()
public void ThrowsCustomExceptionWhenSuppliedGivenZeroFloatValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(0f, "floatZero", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(0f, "floatZero", exceptionCreator: () => customException));
}
@@ -84,7 +84,7 @@ public void ThrowsGivenZeroDoubleValue()
public void ThrowsCustomExceptionWhenSuppliedGivenZeroDoubleValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(0.0, "doubleZero", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(0.0, "doubleZero", exceptionCreator: () => customException));
}
@@ -98,7 +98,7 @@ public void ThrowsGivenZeroTimeSpanValue()
public void ThrowsCustomExceptionWhenSuppliedGivenZeroTimeSpanValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.Zero, "timespanZero", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.Zero, "timespanZero", exceptionCreator: () => customException));
}
@@ -113,8 +113,8 @@ public void ThrowsGivenNegativeIntValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeIntValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(-1, "intNegative", exception: customException));
- Assert.Throws(() => Guard.Against.NegativeOrZero(-42, "intNegative", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(-1, "intNegative", exceptionCreator: () => customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(-42, "intNegative", exceptionCreator: () => customException));
}
@@ -129,8 +129,8 @@ public void ThrowsGivenNegativeLongValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeLongValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(-1L, "longNegative", exception: customException));
- Assert.Throws(() => Guard.Against.NegativeOrZero(-456L, "longNegative", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(-1L, "longNegative", exceptionCreator: () => customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(-456L, "longNegative", exceptionCreator: () => customException));
}
@@ -145,8 +145,8 @@ public void ThrowsGivenNegativeDecimalValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeDecimalValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(-1M, "decimalNegative", exception: customException));
- Assert.Throws(() => Guard.Against.NegativeOrZero(-567M, "decimalNegative", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(-1M, "decimalNegative", exceptionCreator: () => customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(-567M, "decimalNegative", exceptionCreator: () => customException));
}
@@ -163,8 +163,8 @@ public void ThrowsGivenNegativeFloatValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeFloatValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(-1f, "floatNegative", exception: customException));
- Assert.Throws(() => Guard.Against.NegativeOrZero(-4567f, "floatNegative", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(-1f, "floatNegative", exceptionCreator: () => customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(-4567f, "floatNegative", exceptionCreator: () => customException));
}
[Fact]
@@ -179,8 +179,8 @@ public void ThrowsGivenNegativeDoubleValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeDoubleValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(-1.0, "doubleNegative", exception: customException));
- Assert.Throws(() => Guard.Against.NegativeOrZero(-456.453, "doubleNegative", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(-1.0, "doubleNegative", exceptionCreator: () => customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(-456.453, "doubleNegative", exceptionCreator: () => customException));
}
[Fact]
@@ -194,8 +194,8 @@ public void ThrowsGivenNegativeTimeSpanValue()
public void ThrowsCustomExceptionWhenSuppliedGivenNegativeTimeSpanValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.FromSeconds(-1), "timespanNegative", exception: customException));
- Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.FromSeconds(-456), "timespanNegative", exception: customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.FromSeconds(-1), "timespanNegative", exceptionCreator: () => customException));
+ Assert.Throws(() => Guard.Against.NegativeOrZero(TimeSpan.FromSeconds(-456), "timespanNegative", exceptionCreator: () => customException));
}
[Fact]
diff --git a/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs b/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs
index 2e6aadd..45404be 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstNotFound.cs
@@ -28,7 +28,7 @@ public void ThrowsCustomExceptionWhenSuppliedGivenNullValue()
{
object obj = null!;
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NotFound(1, obj, "null", exception: customException));
+ Assert.Throws(() => Guard.Against.NotFound(1, obj, "null", exceptionCreator: () => customException));
}
[Fact]
diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs
index fb94981..69c6f61 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs
@@ -59,7 +59,7 @@ public void ThrowsGivenEmptyStringSpan()
public void ThrowsCustomExceptionWhenSuppliedGivenEmptyStringSpan()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.Empty("".AsSpan(), "emptyStringSpan", exception: customException));
+ Assert.Throws(() => Guard.Against.Empty("".AsSpan(), "emptyStringSpan", exceptionCreator: () => customException));
}
[Fact]
diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs
index 691339d..f2df77d 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForClassIComparable.cs
@@ -54,9 +54,9 @@ public void ThrowsGivenOutOfRangeValue()
public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(-1), "index", new TestObj(1), new TestObj(3), exception: customException));
- Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(0), "index", new TestObj(1), new TestObj(3), exception: customException));
- Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(4), "index", new TestObj(1), new TestObj(3), exception: customException));
+ Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(-1), "index", new TestObj(1), new TestObj(3), exceptionCreator: () => customException));
+ Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(0), "index", new TestObj(1), new TestObj(3), exceptionCreator: () => customException));
+ Assert.Throws(() => Guard.Against.NullOrOutOfRange(new TestObj(4), "index", new TestObj(1), new TestObj(3), exceptionCreator: () => customException));
}
[Fact]
diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs
index 1e36985..a7c76d1 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForInt.cs
@@ -32,7 +32,7 @@ public void ThrowsGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo)
public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int input, int rangeFrom, int rangeTo)
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo, exception: customException));
+ Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException));
}
[Theory]
diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs
index c9bec17..a5b8e9b 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfRangeForNullableInt.cs
@@ -32,7 +32,7 @@ public void ThrowsGivenOutOfRangeValue(int? input, int rangeFrom, int rangeTo)
public void ThrowsCustomExceptionWhenSuppliedGivenOutOfRangeValue(int? input, int rangeFrom, int rangeTo)
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo, exception: customException));
+ Assert.Throws(() => Guard.Against.NullOrOutOfRange(input, "index", rangeFrom, rangeTo, exceptionCreator: () => customException));
}
[Theory]
@@ -65,7 +65,7 @@ public void ThrowsGivenInvalidNullArgumentValue()
public void ThrowsCustomExceptionWhenSuppliedGivenInvalidNullArgumentValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NullOrOutOfRange(null, "index", -10, 10, exception: customException));
+ Assert.Throws(() => Guard.Against.NullOrOutOfRange(null, "index", -10, 10, exceptionCreator: () => customException));
}
}
diff --git a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs
index 3b18598..0573b22 100644
--- a/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs
+++ b/test/GuardClauses.UnitTests/GuardAgainstNullOrOutOfSQLDateRange.cs
@@ -34,7 +34,7 @@ public void ThrowsCustomExceptionWhenSuppliedGivenValueBelowMinDate(int offsetIn
DateTime date = SqlDateTime.MinValue.Value.AddSeconds(-offsetInSeconds);
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(date, nameof(date), exception: customException));
+ Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(date, nameof(date), exceptionCreator: () => customException));
}
@@ -106,7 +106,7 @@ public void ThrowsGivenInvalidNullArgumentValue()
public void ThrowsCustomExceptionWhenSuppliedGivenInvalidNullArgumentValue()
{
Exception customException = new Exception();
- Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(null, "index", exception: customException));
+ Assert.Throws(() => Guard.Against.NullOrOutOfSQLDateRange(null, "index", exceptionCreator: () => customException));
}
public static IEnumerable