How to handle an exception occurring in a filter?

I have various filters before a URL hits an controller action.

Very few filters have a complex logic which might throw exceptions.

Now, how do I catch these exceptions ?

For eg. I have a filter which handles exception occuring in a controller method.

[ActionFilter1] is a filter which handles any exception occuring in a controller method.

public override void OnActionExecuting(ActionExecutingContext filterContext)
{

 //exception occuring here

}

One way is to do something like this:

public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
     try {
     //exception occuring here
     }
     catch {
     //log here
     }
    }

since there are n filters, I do not want to repeat this logic of adding try & catch to each and every filter.

In that case, how can I proceed with a single place error handling which can handle any exceptions occurring in all these filters ?

Upvotes: 6

Views: 12626

Answers (2)

Adding to Divya's answer/comments, here's my find:

Code snippet:

[Filter1, Filter2, MyErrorFilter]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //throw new Exception("Poda!");
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }

    public class Filter1 : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Console.WriteLine("filter1-action executing!!");
            base.OnActionExecuting(filterContext);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            Console.WriteLine("filter1-action executed!!!");
            base.OnActionExecuted(filterContext);
        }
    }

    public class Filter2: ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Console.WriteLine("filter2-action executing!!!");
            throw new Exception("poda!!1");
            base.OnActionExecuting(filterContext);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            Console.WriteLine("filter2-action executed!!!");
            base.OnActionExecuted(filterContext);
        }
    }

    public class MyErrorFilter : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            Console.WriteLine("an exception captured!!!");
            base.OnException(filterContext);
        }
    }

Now, the exception is occuring at Filter2 which is being captured by HandleErrorAttribute (This filter has an order of -1, btw)

So, even If you have n filters, if any of the exceptions occur, then it is captured at the HandleErrorAttribute level :)

Note[2017 Nov]: You can register this MyErrorFilter at a global level so you need not decorate it on top of every controller you have.

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new MyErrorFilter());
    }
}

Upvotes: 2

divya
divya

Reputation: 304

Read this article. You will find the answer http://www.codeproject.com/Articles/850062/Exception-handling-in-ASP-NET-MVC-methods-explaine

Upvotes: 5

Related Questions