Alex Rivera | Logout

ASP.NET MVC Global error handling

Asked 2012-02-27T07:05:20.410
11

I have a custom HandleError attribute that deals with errors on the MVC pipeline; I have an protected void Application_Error(object sender, EventArgs e) method on my Global.asax which handles errors from outside the pipeline.

I've come across an scenario I didn't know was possible; In implementing DI, there's a dependency for a connectionString, which is taken from the application configuration file.

As the connection string didn't exist yet, an error raises when creating the controller, this usually makes the Application_Error handler fire, and a proper error page is rendered (through rendering a partial view as string and sending it as the response, and in case this fails it just writes "Fatal exception." to the response.

Except in this case, I get the fugly default ASP.NET "runtime error" yellow screen of death. Telling me:

Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.

Details: To enable the details of this specific error message to be viewable on the local server machine, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".

I don't have a defaultRedirect set in my customErrors, nor is it turned Off, because I don't want to redirect but to render errors on the same page the user is in, avoiding a needless redirect.

How can I handle an scenario such as this? And what's even the reason why it behaves this way and not like any other error outside a controller?

I realize it'

Edit
Report

1 Answer

0

You haven't shown how exactly are you handling the errors in your Application_Error event nor how does your custom HandleAttribute is implemented so it's hard to guess what the problem might be. A common problem to watch for when implementing Application_Error is that you render some error view which itself throws an error. Think for example an error view that depends on a Layout and inside this layout you are calling a Html.Action helper to render the contents of another action that itself does database access and stuff. Your error views should be as static as possible. Ideally you should have a different Layout for them to avoid those kind of situations.

I may also suggest you taking a look at the following approach for handling errors.

answered 2012-02-27T07:20:55.000

Your Answer