MVC HandleError filter didn't catch an exception
Asked Answered
M

1

2

I've an MVC 3 web app in which I'm using "HandleError" Action Filter for exception handling. I've this action filter implemented as follows:

[HandleError]
public class BaseController : Controller {...}

This is the base class from which all of my controllers are derived. In my web.config I've and there's an Error.cshtml in my Shared folder (.cshtml because I use Razor). Everything has been working fine and I get a fine exception handling (formatted by my function)

Recently, somehow I got and "unhandled exception (YSOD)" and because of "customErrors" I got the default ASP.Net error message which didn't have any info about the actual exception. This happened in an AJAX post back. However, I'm unable to reproduce it.

Is it possible for any sort of errors to escape this action filter?

Migraine answered 29/8, 2012 at 17:48 Comment(0)
H
4

Is it possible for any sort of errors to escape this action filter?

HandleError filter doesn't catch all the exceptions fired in an application. It can capture exceptions that are fired inside actions, action filters.. simply inside the MVC context. Also it doesn't capture HTTP exceptions having status code other than 500. Relying only on HandleError filter in an MVC application is a bad idea.

You should still rely on the Application_Error event to do some logging and customErrors section to display a custom error page for the exceptions that are not captured by HandleError.

I've written a blog post on this subject that may help you.

Howse answered 30/8, 2012 at 11:56 Comment(9)
Mark, sorry for late. Yes, I've come across some exceptions like "UnauthorizedAccessException" (possibly wile file access). I agree Application_Error would probably the last stage to catch it. However, [HandleError] is more like MVC - how about this post (https://mcmap.net/q/194554/-asp-net-mvc-handleerror - 3rd answer) which allows to capture such exceptions. ELMAH seems fine for logging.Migraine
I tried your Application_Error code yesterday and it was unable to handle "UnauthorizedAccessException". Later after I changed the code to controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction); - it worked! I wanted to know if this co-exists with [HandleError] or would you say that after Application_error the [HandleError] is no longer needed.Migraine
It should be controller.ViewData.Model = new HandleErrorInfo()Howse
Before I mark your answer, I just wanted to confirm whether this is the best resort? I mean if its just about setting the status code or is Application_Error a sure place where the error would land. And one last thing - hope it can work in parallel with ELMAH (for logging)Migraine
Compared to HandleError I'll say Application_Error is a better option. It's not about setting the status code it's about catching all the exceptions created in an application. If you are going to use ELMAH then I don't think why you want to use Application_Error because ELMAH will handle all the unhandled exceptions and log them. Compared to Application_Error I may suggest ELMAH but only thing is you want to work out how to return different views for different errors (may be customErrors).Howse
Yes of course, if we go with ELMAH then I would not need to handle anything at all (hopefully). And without it I believe Application_Error would be suffice. Thank you.Migraine
Hey Mark - if you're still listening - I'm not able to show a "formatted exception" with ELMAH. I'm able to configure the error log which lists all the errors and shows details when clicked. But I need to show formatted exception details when an exception occurs - does ELMAH have that ? Or do I have to persist Application_ErrorMigraine
@HemantTank Can you post this as a separate question? so others also can help you.Howse
I agree, this one has overgrown. Here you go - #12412714Migraine

© 2022 - 2024 — McMap. All rights reserved.