3

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?

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just realised that myself. I use something along those lines: ValidationFailure test = new ValidationFailure("bla", "bla"); errors.Add(test); throw new ValidationException(errors);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.