I created a new class which inherits from the HandleErrorAttribute to handle ajax requests differently. The only override is on the OnException method:
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
filterContext.Result = new JsonResult { Data = errorText };
return;
}
base.OnException(filterContext);
}
This does work, about half the time. When it works, the status code in the response is 500 and the error message is provided. When it doesn't, the status code is 12031 and the error message is empty.
Apparently status code 12031 is means:
ERROR_INTERNET_CONNECTION_RESET The connection with the server has been reset.
No idea why this would be occurring.