views:

123

answers:

2

When an exception occurs in an action called from an Ajax Request, the default HandleError filter can not work correctly. Can someone help me solve it?

A: 

I go in this way: in the action that serves the ajax request I catch all the possible errors and return an JSon object with the errors that appeared. So the idea is very simple to write try..catch blocks in this action code to get all the possible errors and log them if necessary.

diadiora
+1  A: 

You can also override the OnException method in your controller to catch all unhandled exceptions and return a custom view or JSON/XML in case of an error.

    protected override void OnException(ExceptionContext filterContext) {
        filterContext.ExceptionHandled = true;
        filterContext.Result = this.Json(new {
            result = "error",
            details = filterContext.Exception.GetType().Name + ": " + filterContext.Exception.Message
        });
    }

See this post for more info on returning a custom view: http://geekswithblogs.net/SanjayU/archive/2009/11/09/error-handling-in-asp.net-mvc-1-part-2-of-2.aspx

rusvdw