I am using the following code to get exceptions from my MVC Model State:
IEnumerable<string> exceptions = ModelState (x => x.Value.Errors
.Where(error => (error != null) &&
(error.Exception != null) &&
(error.Exception.Message != null))
.Select(error => error.Exception.Message));
For try-catch block exceptions how could I code a way to take the exception e and make it so my exception and inner exception messages were also put into an IEnumerable like I am doing above?
My problem is that I am not sure how to handle the fact that there could be multiple levels of exceptions.
Not sure if this helps but here’s the code I was using before to put exceptions into a string:
public static string GetFormattedErrorMessage(this Exception e)
{
var exError = "";
if ( 1 == 1) {
if (e == null)
{
throw new ArgumentNullException("e");
}
exError = e.Message;
if (e.InnerException != null)
{
exError += "<br>" + e.InnerException.Message;
exError += "<br>" + e.StackTrace;
if (e.InnerException.InnerException != null)
{
exError += "<br>" + e.InnerException.InnerException.Message;
}
}
} else {
exError = "System error";
}
return exError;
}
You could traverse down the inner exceptions e.g.