4

I'm trying to handle all ajax exceptions globally in my application.

I have a simple validation exception that is thrown after an ajax post request is sent to the server.

if (string.IsNullOrEmpty(name)) { throw new Exception("Name cannot be empty."); }

I have a class the overrides the OnException method:

public override void OnException(ExceptionContext filterContext)
{
        if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
        {
            return;
        }

        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            filterContext.Result = new JsonResult
            {
                Data = new { message = filterContext.Exception.Message },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
        }
        else
        {
            base.OnException(filterContext);
        }
 }

And a javascript function that listens to all ajax errors:

$(document).ajaxError(function(e, xhr, settings, exception) {
    e.stopPropagation();
    if (xhr.responseJSON != null) {
        showMessage(xhr.responseJSON.message, ERROR);
    }
});

The problem is, on my Azure server which is configured for https, the xhr.responseJSON is not returned. However, it works fine locally and I am able to display the exception message thrown. Is https somehow blocking the request? I have tried locally to run my application through https but I'm not able to recreate the issue.

I'm intending to use the same methodology for much more than just validation exceptions, as I know they can be easily handled from the client.

1 Answer 1

5

I had the same issue.

Adding filterContext.HttpContext.Response.TrySkipIisCustomErrors = true fixed the issue for me.

So in your case:

public override void OnException(ExceptionContext filterContext)
{
        if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
        {
            return;
        }

        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            filterContext.Result = new JsonResult
            {
                Data = new { message = filterContext.Exception.Message },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();

            // Added this line
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true

        }
        else
        {
            base.OnException(filterContext);
        }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. A great help in resolving my JavaScript error handling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.