Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Ajax Error returning view instead of ajax

I'm making an ASP.NET MVC call to a method via AJAX and the error throws an exception. I'd like the message of the exception to be passed back to the client, and I'd prefer not to have to catch the exception. Something like this:

[HttpPost]
public ActionResult AddUser(User user) {
  if (UserIsValid(user)) {
    return Json(new { resultText = "Success!" });
  } 
  throw new Exception("The user was invalid.  Please fill out the entire form.");
}

I'm seeing in my firebug response an HTML page

<!DOCTYPE html>
<html>
    <head>
        <title>"The user was invalid.  Please fill out the entire form."</title>
        .....

I'd like to not be forced to use a try catch block to do this. Is there a way to automatically get the jQuery $(document).ajaxError(function () {} to read in this exception message? Is this bad practice? Can I override the controller OnException? Or do I have to try/catch and return JSON?

Something like this would be nice:

$(document).ajaxError(function (data) {
    alert(data.title);        
});
like image 640
Adam Levitt Avatar asked Feb 14 '23 20:02

Adam Levitt


1 Answers

You can do this with a custom filter:

$(document).ajaxError(function(event, jqxhr) {
    console.log(jqxhr.responseText);
});

-

[HttpPost]
[CustomHandleErrorAttribute]
public JsonResult Foo(bool isTrue)
{
    if (isTrue)
    {
        return Json(new { Foo = "Bar" });
    }
    throw new HttpException(404, "Oh noes...");
}

public class CustomHandleErrorAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        var exception = filterContext.Exception;
        var statusCode = new HttpException(null, exception).GetHttpCode();

        filterContext.Result = new JsonResult
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet, //Not necessary for this example
            Data = new
            {
                error = true,
                message = filterContext.Exception.Message
            }
        };

        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = statusCode;  
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }
}

Somewhat inspired by this blogpost: http://www.prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc

like image 161
Francis Avatar answered Feb 17 '23 10:02

Francis