Question

I have defined an:

IValidator<SomeClass> _myValidator

I can do:

_myValidator.ValidateAndThrow(someObject);

instead I would like to:

var errors = _myValidator.Validate(entity);

add some errors manually and then re-throw the errors. Is this possible?

Was it helpful?

Solution

If you want to store the errors in a variable, you'll have to return the error, or a list of errors, instead of throwing them.

IList<Exception> ValidateAndThrow(object someObject){

    IList<Exception> errors = new List<Exception>();

    try{
        SomethingGoesWrong();
    } catch (Exception e){
        errors.Add(e);
    }

    return errors;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top