×
Get in touch

Get in touch with Binary

Binary Studio website uses cookies to enhance your browsing experience. By continuing to use our site, you agree to our Privacy Policy and use of cookies.

Learn more more arrow
Agree
kirill.buga@binary-studio.com'
Kirill Buga JS developer 28.05.2015

Error Handling in MVC Applications


Error handling is a widespread task in the most applications. This aspect is incredibly important while user after seeing popup message with Critical error and a bunch of strange symbols will close this application and will avoid to open it ever again. And not less attention should be paid to architectural decision that will allow to use global error handler instead of using copied code overall.
By default in MVC applications (v.2+) you have possibility to switch on standard error handler. To do this you should simply add next line to config's section :

1

Let's add action to home controller to check this:

When you call this action you will get something like this:

2

After that user gets heart attack and switches to mode “it wasn’t me”. Now we switch flag to “on” and try once more.

3

This time our exception was handled and user got some clear (or almost clear) error-message.

How does it work? Secret is in default configuration of MVC-application.

4
On start application calls method RegisterGlobalFilters() which performs following action:

This is the one that handles exceptions in MVC applications' actions. By default all errors are rendered to “Error” view (that can be found in ~/Views/Shared). Behavior of HandleErrorAttribute can be configured through following attributes:

  • ExceptionType. Specifies the exception type or types that the filter will handle. If this property is not specified, the filter handles all exceptions.
  • View. Specifies the name of the view to display.
  • Master. Specifies the name of the master view to use, if any.
  • Order. Specifies the order in which the filters are applied, if more than oneHandleErrorAttribute filter is possible for a method.

MVC framework also adds information about exceptions occurred in ViewDataDictionary instance, where Model will contain exemplar of ExceptionContext. The following information is stored In dictionary ViewData:

  • ActionName. The intended action method.
  • ControllerName. The intended controller.
  • Exception. The exception object.

Method described before is very easy to implement, but HandleErrorAttribute catches only unhandled controllers' exception. If exception was raised in some other place then user will get that scary error message shown before or error page, set up in customErrors in Web.config. Standard mechanism works fine for restricted class of tasks, but sometimes full control for exception handling is needed.
ASP.NET provides few levels for error handling in the application. There are 3 methods to do this: Page_Error, Application_Error and configuration file (Web.config mentioned before).
Let’s use method Application_Error in Global.asax and redirect application to expected actions. First of all we create ErrorController, that will perform redirection, and few actions inside. Below you can see complete implementation:

Now we will write extension method for Exception class that will try to get ErrorCode and redirect call accordingly to ErrorController

 

So, in this method we’ll try to cast exception to HttpException after that we’ll read error code and define action for redirection. And for catching excptions we’ll use Application_Error.

Don’t forget to set flag in web.config to default value.

<customErrorsmode="Off">

That’s all. When we try to raise 404 error our application will redirect us to the page needed

5

1.http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx
2.http://support.microsoft.com/kb/306355/ru
3.http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/handleerrorattribute-or-error-handling-in-mvc-4/
4.https://gist.github.com/confa/3339210