Skip to content

Conversation

UnleashedGH
Copy link
Contributor

This new optional parameter can be supplied to the IGuardClause methods to throw the supplied exception instead of the default one in the method.

it's usage would be something like this:

var emplyeeName = null;
Guard.Against.Null(emplyeeName , exception: new EmployeeNameException());

the custom exception would only be thrown if exception relates to what the method checks and not thrown for param validation for the method itself

for instance, the custom exception would not be thrown here instead of ArgumentException because that validates the params (rangeFrom and rangeTo) for the method itself

    if (rangeFrom.CompareTo(rangeTo) > 0)
    {
        throw new ArgumentException(message ?? $"{nameof(rangeFrom)} should be less or equal than {nameof(rangeTo)}", parameterName);
    }

    if (input.Any(x => x.CompareTo(rangeFrom) < 0 || x.CompareTo(rangeTo) > 0))
    {
        if (string.IsNullOrEmpty(message))
        {
            throw exception ?? new ArgumentOutOfRangeException(parameterName, message ?? $"Input {parameterName} had out of range item(s)");
        }
        throw exception ?? new ArgumentOutOfRangeException(parameterName, message);
    }

A custom exception can be supplied to the guard clause methods to throw it instead of the default exception
A custom exception can be supplied to the guard clause methods to throw it instead of the default exception
Also fixed a few named arguments missing from method calls that i found through running the tests
@ardalis
Copy link
Owner

ardalis commented Mar 21, 2024

Awesome thanks! I'll try to get this out tomorrow.

@ardalis
Copy link
Owner

ardalis commented Apr 19, 2024

It's a little weird that you're newing up the exception every time even if the guard doesn't throw. Seems like a Func where T:Exception would potentially be a better approach and then you only instantiate the exception as needed. Going to merge this and then check that out.

@ardalis ardalis merged commit b4771e5 into ardalis:main Apr 19, 2024
@ardalis
Copy link
Owner

ardalis commented Apr 28, 2024

I'm in the process of replacing the Exception parameter with a factory method that will produce the exception only when needed, which should be better performing and have fewer allocations.

@mwasson74
Copy link

I'm in the process of replacing the Exception parameter with a factory method that will produce the exception only when needed, which should be better performing and have fewer allocations.

@ardalis how's this coming along? 😬

@ardalis
Copy link
Owner

ardalis commented Jun 19, 2024

I started on it a while ago and then got busy with other things. You can see the WIP here:
6186c68

Basically I want to continue that pattern throughout before I create another release. Anyone want to help get this to the finish line?

@mwasson74
Copy link

Maybe @UnleashedGH can...? 🤞

@ardalis
Copy link
Owner

ardalis commented Jun 25, 2024

Maybe @canro91 can take a look...

This was referenced Sep 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants