views:

169

answers:

1

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.

A: 

I'm not sure precisely what is going on here, but I can make an educated guess. We do something very similar, except we call base.OnException first, and then we test filterContext.ExceptionHandled to see if the base filter handled the exception. This has always worked for us, and is different than what you do. Note that the base filter will not always handle the exception (see the source code for details).

Note that the base filter does things which your filter does not do, like test IsCustomErrorEnabled and the specific type of the exception. There are some exceptions which should not be handled; again, refer to the MVC source code for details on this.

So my wild guess is that in some circumstances your filter (which always handles the exception when there is an AJAX request) and the base filter do different things. I would suggest that you try the method we are using (call base first, and only perform your custom AJAX handling if the request is an AJAX request and the base has indicated that the exception should be handled) and see if that doesn't end up working better for you.

Craig Stuntz
Tried your suggestions. Still not working. I did notice that I can only reproduce this issue when throwing an exception from within an action method.
chief7