I currently handle errors in my MVC application with a global error handler. In cases where I am getting DbEntityValidationException, I would like to be able to drill down to see the errors. I know this is possible in a catch statement, but I would like to access this data from my global error handler which uses the getLastError method. Is it possible to get these errors?
Here is my global error handler code:
protected void Application_Error()
{
//Global error handler
HttpContext ctx = HttpContext.Current;
Exception ex = ctx.Server.GetLastError();
ctx.Response.Clear();
RequestContext rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;
ViewResult viewResult = new ViewResult { ViewName = "Error" };
viewResult.ViewBag.Error = ErrorManagement.getErrorMessage(ex); //this does the heavy listing with db and email
string controllerName = rc.RouteData.GetRequiredString("controller");
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(rc, controllerName);
ControllerContext cc = new ControllerContext(rc, (ControllerBase)controller);
viewResult.ExecuteResult(cc);
ctx.Server.ClearError();
}
So I am hoping I can get the error info from within this Exception object:
Exception ex = ctx.Server.GetLastError();
I realize, as per this article, Validation failed for one or more entities. See 'EntityValidationErrors' property for more details that I can write an extension method for the db.Save method but I was hoping to avoid this if possible.
Thanks! You're help is much appreciated.