Create your own exception class that can store the data you need:
public class AccountException : ApplicationException { public Dictionary<string, string> Errors { get; set; }; public AccountException(Exception ex) : base(ex) { Errors = new Dictionary<string, string>(); } public AccountException() : this(null) {} }
In your methods, you can throw an exception. Also do not return the error status that is handled by the exception.
Do not throw the exception that you get in the method, include it as an InnerException so that it can be used for debugging.
public void Delete(Account account) { try { _accountRepository.Delete(account); } catch(Exception ex) { AccountException a = new AccountException(ex); a.Errors.Add("", "Error when deleting account"); throw a; } } public void ValidateNoDuplicate(Account ac) { var accounts = GetAccounts(ac.PartitionKey); if (accounts.Any(b => b.Title.Equals(ac.Title) && !b.RowKey.Equals(ac.RowKey))) { AccountException a = new AccountException(); a.Errors.Add("Account.Title", "Duplicate"); throw a; } }
When calling methods, you will catch your type of exception:
try { Delete(account); } catch(AccountException ex) {
source share