Last active
          March 8, 2016 01:18 
        
      - 
      
 - 
        
Save justin-edwards/4969710 to your computer and use it in GitHub Desktop.  
    Debugging: throw all validation error messages and exceptions from modelstate
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | if (!ModelState.IsValid) | |
| { | |
| var sb = new System.Text.StringBuilder(); | |
| Dictionary<string, IEnumerable<Exception>> errorList = ModelState.ToDictionary( | |
| kvp => kvp.Key, | |
| kvp => kvp.Value.Errors.Select(e => e.Exception) | |
| ); | |
| sb.AppendLine("Exceptions:"); | |
| foreach (string key in errorList.Keys) | |
| { | |
| foreach (Exception ex in errorList[key]) | |
| { | |
| if (ex != null) | |
| { | |
| sb.AppendFormat("{0}: {1} - [{3}]{2}", key, ex.Message, Environment.NewLine, ex.StackTrace); | |
| } | |
| } | |
| } | |
| sb.AppendLine("Validation:"); | |
| Dictionary<string, IEnumerable<string>> errorMessages = ModelState.ToDictionary( | |
| kvp => kvp.Key, | |
| kvp => kvp.Value.Errors.Select(e => e.ErrorMessage) | |
| ); | |
| foreach (string key in errorMessages.Keys) | |
| { | |
| foreach (string message in errorMessages[key]) | |
| { | |
| sb.AppendFormat("{0}: {1}{2}", key, message, Environment.NewLine); | |
| } | |
| } | |
| throw new Exception(sb.ToString()); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment