Skip to content

Instantly share code, notes, and snippets.

@justin-edwards
Last active March 8, 2016 01:18
Show Gist options
  • Save justin-edwards/4969710 to your computer and use it in GitHub Desktop.
Save justin-edwards/4969710 to your computer and use it in GitHub Desktop.
Debugging: throw all validation error messages and exceptions from modelstate
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