private void AccountValidations(CreateAccountPayload payload) {
if (string.IsNullOrEmpty(payload.Note)) {
throw new ArgumentException($ "Note cannot be empty");
}
if (string.IsNullOrEmpty(payload.AccountName)) {
throw new ArgumentException($ "Account Name cnnot be Empty");
}
if (string.IsNullOrEmpty(payload.Type)) {
throw new ArgumentException($ "Account Type cnnot be Empty");
}
}
I want all the exception messages at once, eg: In the payload object if I don't provide AccountName and Note. It should report me both Note cannot be empty and Account Name can not be Empty How can I do that?
I thought of making a List of all these messages and then throw a Agregateexception. How can I do this?
Well, to validate your
CreateAccountPayloadyou can do the following.A. You can indeed throw the
AggregateExceptionbut first you need to add your exceptions to the list.The outer code should catch the exception.
You just need to inspect the
InnerExceptionsproperty and construct the errors string like thisB. Another option might be the following. You can add your messages (not throwing exceptions) to a List of strings, and return it. And if the list is empty - validation passed.