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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Hemant Tank
  • 1,724
  • 4
  • 28
  • 56

1 Answers1

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.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
VJAI
  • 32,167
  • 23
  • 102
  • 164
  • 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 (http://stackoverflow.com/questions/183316/asp-net-mvc-handleerror - 3rd answer) which allows to capture such exceptions. ELMAH seems fine for logging. – Hemant Tank Sep 04 '12 at 19:12
  • 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. – Hemant Tank Sep 08 '12 at 07:10
  • It should be controller.ViewData.Model = new HandleErrorInfo() – VJAI Sep 08 '12 at 10:41
  • 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) – Hemant Tank Sep 10 '12 at 13:30
  • 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). – VJAI Sep 11 '12 at 03:30
  • 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. – Hemant Tank Sep 11 '12 at 15:23
  • 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_Error – Hemant Tank Sep 13 '12 at 15:32
  • @HemantTank Can you post this as a separate question? so others also can help you. – VJAI Sep 13 '12 at 15:41
  • I agree, this one has overgrown. Here you go - http://stackoverflow.com/questions/12412214/mvc-elmah-v-s-application-error – Hemant Tank Sep 13 '12 at 18:06