I have an override OnException(ExceptionContext filterContext) in my base controller to catch the app during any errors, and then log them. The problem I'm getting in my app is this particular method is fired off four times for certain errors. I'll walk you through a scenario:
Let's say i navigate to: http://localhost:180/someController/someAction?someId=XX
And I have poor object handling in my code. The Id passed in is an invalid one, and it retrieves some null object, I then, bc of my bad object handling, try to operate on a null object. I get an exception.
BaseController's OnException is fired off here.
That null object is still returned out to the view, where the view tries to bind it to something, or what have you.
BaseController's OnException is fired off here again, for the error in the view.
Essentially, only one error is important to me, but the trickle up effect is causing more errors to fire off, and spam my inbox :-/.
What is the correct way to catch an error in MVC2 and not have this happen to me?
I would recommend you inheriting from the HandleError attribute and rolling your exception handling in there. Overriding the OnException on a single controller means you either have a lot of exception handling code in a lot of controllers or your inherit from a base one, which due to the MVC pipeline is not really necessary in either case.
By using the attribute, you should have one occurrence of an error per action executed, and once the error is handled it won't fire again. Hopefully this will cut down on repeat exception messages.
I personally use attributes for the exception handling cause it's cleaner and more reusable and get's rid of a lot of noise within my actions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With