Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Func to pass custom exception to Guard methods #350

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/GuardClauses/GuardAgainstEmptyOrWhiteSpaceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static partial class GuardClauseExtensions
/// <param name="input"></param>
/// <param name="parameterName"></param>
/// <param name="message">Optional. Custom error message</param>
/// <param name="exception">Optional. Custom exception</param>
/// <param name="exceptionCreator">Optional. Custom exception</param>
/// <returns><paramref name="input" /> if the value is not an empty string.</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="Exception"></exception>
Expand All @@ -21,10 +21,12 @@ public static ReadOnlySpan<char> Empty(this IGuardClause guardClause,
ReadOnlySpan<char> input,
string parameterName,
string? message = null,
Exception? exception = null)
Func<Exception>? 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;
Expand All @@ -37,17 +39,19 @@ public static ReadOnlySpan<char> Empty(this IGuardClause guardClause,
/// <param name="input"></param>
/// <param name="parameterName"></param>
/// <param name="message">Optional. Custom error message</param>
/// <param name="exception">Optional. Custom exception</param>
/// <param name="exceptionCreator">Optional. Custom exception</param>
/// <returns><paramref name="input" /> if the value is not an empty or whitespace string.</returns>
/// <exception cref="ArgumentException"></exception>
public static ReadOnlySpan<char> WhiteSpace(this IGuardClause guardClause,
ReadOnlySpan<char> input,
string parameterName,
string? message = null,
Exception? exception = null)
Func<Exception>? exceptionCreator = null)
{
if (MemoryExtensions.IsWhiteSpace(input))
{
Exception? exception = exceptionCreator?.Invoke();

throw exception ?? new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName!);
}

Expand Down
12 changes: 8 additions & 4 deletions src/GuardClauses/GuardAgainstExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static partial class GuardClauseExtensions
/// <param name="input">The input to evaluate.</param>
/// <param name="message">The message to include in the exception if the input is invalid.</param>
/// <param name="parameterName">The name of the parameter to include in the thrown exception, captured automatically from the input expression.</param>
/// <param name="exception"></param>
/// <param name="exceptionCreator"></param>
/// <returns>The <paramref name="input"/> if the <paramref name="func"/> evaluates to false, indicating a valid state.</returns>
/// <exception cref="ArgumentException">Thrown when the validation function returns true, indicating that the input is invalid.</exception>
/// <exception cref="Exception"></exception>
Expand All @@ -26,11 +26,13 @@ public static T Expression<T>(this IGuardClause guardClause,
T input,
string message,
[CallerArgumentExpression("input")] string? parameterName = null,
Exception? exception = null)
Func<Exception>? exceptionCreator = null)
where T : struct
{
if (func(input))
{
Exception? exception = exceptionCreator?.Invoke();

throw exception ?? new ArgumentException(message, parameterName!);
}

Expand All @@ -48,7 +50,7 @@ public static T Expression<T>(this IGuardClause guardClause,
/// <param name="input">The input to evaluate.</param>
/// <param name="message">The message to include in the exception if the input is invalid.</param>
/// <param name="parameterName">The name of the parameter to include in the thrown exception, captured automatically from the input expression.</param>
/// <param name="exception"></param>
/// <param name="exceptionCreator"></param>
/// <returns><paramref name="input"/> if the <paramref name="func"/> evaluates to true </returns>
/// <exception cref="ArgumentException">Thrown when the validation function returns true, indicating that the input is invalid.</exception>
/// <exception cref="Exception"></exception>
Expand All @@ -57,11 +59,13 @@ public static async Task<T> ExpressionAsync<T>(this IGuardClause guardClause,
T input,
string message,
[CallerArgumentExpression("input")] string? parameterName = null,
Exception? exception = null)
Func<Exception>? exceptionCreator = null)
where T : struct
{
if (await func(input))
{
Exception? exception = exceptionCreator?.Invoke();

throw exception ?? new ArgumentException(message, parameterName!);
}

Expand Down
18 changes: 12 additions & 6 deletions src/GuardClauses/GuardAgainstInvalidFormatExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static partial class GuardClauseExtensions
/// <param name="parameterName"></param>
/// <param name="regexPattern"></param>
/// <param name="message">Optional. Custom error message</param>
/// <param name="exception"></param>
/// <param name="exceptionCreator"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="Exception"></exception>
Expand All @@ -23,11 +23,13 @@ public static string InvalidFormat(this IGuardClause guardClause,
string parameterName,
string regexPattern,
string? message = null,
Exception? exception = null)
Func<Exception>? 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);
}

Expand All @@ -42,7 +44,7 @@ public static string InvalidFormat(this IGuardClause guardClause,
/// <param name="parameterName"></param>
/// <param name="predicate"></param>
/// <param name="message">Optional. Custom error message</param>
/// <param name="exception"></param>
/// <param name="exceptionCreator"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
Expand All @@ -51,10 +53,12 @@ public static T InvalidInput<T>(this IGuardClause guardClause,
T input, string parameterName,
Func<T, bool> predicate,
string? message = null,
Exception? exception = null)
Func<Exception>? exceptionCreator = null)
{
if (!predicate(input))
{
Exception? exception = exceptionCreator?.Invoke();

throw exception ?? new ArgumentException(message ?? $"Input {parameterName} did not satisfy the options", parameterName);
}

Expand All @@ -69,7 +73,7 @@ public static T InvalidInput<T>(this IGuardClause guardClause,
/// <param name="parameterName"></param>
/// <param name="predicate"></param>
/// <param name="message">Optional. Custom error message</param>
/// <param name="exception"></param>
/// <param name="exceptionCreator"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
Expand All @@ -79,10 +83,12 @@ public static async Task<T> InvalidInputAsync<T>(this IGuardClause guardClause,
string parameterName,
Func<T, Task<bool>> predicate,
string? message = null,
Exception? exception = null)
Func<Exception>? exceptionCreator = null)
{
if (!await predicate(input))
{
Exception? exception = exceptionCreator?.Invoke();

throw exception ?? new ArgumentException(message ?? $"Input {parameterName} did not satisfy the options", parameterName);
}

Expand Down
Loading
Loading