views:

1512

answers:

6

How do I implement error handling elegantly? For example, my data access layer can potentially throw 2 types of errors: 1) not authorized access, in which case the page should hide everything and just show the error message 2) errors that inform the user that something like this already exists in the database (say name not unique - for example), and in this case I wouldn't want to hide everything.

EDITED:

As a result of some comments here I devised that I should create derived specialized exception types, such as NotAuthorizedException, DuplicateException, etc etc.... it's all fine and dandy, however I can see 2 problems potentially:

1) Every stored proc has a return field p_error where it contains an error message. Upon getting the data from DB, I need to check this field to see what type of an error has been returned, so I can throw an appropriate exceptions. So, I still need to store my error types/error messages somewhere.....In other words, how do I should the exact message to the user (at certain times I need to) w/o checking the p_error field first. WHich brings me back to error object. Anyone?

2) I can this potentially turning into a nightmare where the number of exceptions equals the number of error message types.

Am I missing something here?

Much thanks to everyone!

+4  A: 

You should check out the exception handling block in Enterprise Library. Lots of good tips and codeware surrounding wrapping exceptions and passing them between layers.

JP Alioto
Thanks, I am still unclear how this would inform the UI.....
gnomixa
You could catch the exception in the UI, you don't have to define your own exception type to catch something.
J.W.
+3  A: 

Where's your business layer, and why isn't it checking Authorization and integrity? The DAL is too low level to be checking those rules - if you hit a problem there, it's pretty much time to throw an exception. Your business layer or controllers can catch that exception, and display a reasonable message - but it's not something you should regularly be doing.

Mark Brackett
Authorization is done through the database, that's not in my control, so I need a way to elegantly pass this from DAL to UI.Secondly, some user input needs to be validated against the database. In which case, DAL will return an error to Business layer, again I need to present this to UI elegantly.
gnomixa
I have one approach in mind, but I would like to see what other people have done.
gnomixa
I agree with the business layer idea... that is what I was trying to communicate in my post... but any way
J.13.L
A: 

One option that I was thinking of using is create an Error class, but then I would need to pass it from UI to business layer and the then to data access layer by reference

I am not sure I understand this. You don't have to pass the error object in every layer. For example, in one of your example, errors that inform the user that something like this already exists in the database (say name not unique - for example) , a sql exception could be thrown by the framework, and you just need to catch the specific exception in your business layer, or UI layer.

Exception handling block by the Enterprise library suggested by other people will allow you define some policy-based exception handling in your web.config file. It could be good place if you want to develop some enterprise application. But for simple application, you may need not go that far.

J.W.
yes, I get that much:) but i need a way to distinguish between fatal errors (such as no access) and data validation errors, aside from checking the error message of course. Is there a better way of doing this?
gnomixa
There are two ways to represent an error, either error code or exception . I am not sure whether you have access to every layer, but you can wrap the error code in your exception class and throw it. Then catch all exception in one place. HTH.
J.W.
A: 

What happens in the upper layers isn't up to your Data Access Layer. It shouldn't even be aware of what the upper layers are going to do. If you've got a duplicate key error, then it should throw something like a "DuplicateKeyException". If you should hit an authorization error (I presume you mean "exception"), then don't do anything with it - let it bubble back up to the UI layer, which can display an appropriate error page.

Remember that error status values and such things are the reason we invented exceptions.

John Saunders
A: 

Enterprise library exception handling block is the bomb as many have pointed out. Using policies you can do things like logging the exception, wrapping it in a different exception, throwing a new exception instead of the original one. Also, if you are looking to perform specific actions based on authentication errors or duplicate record errors etc, you could always create specific derived exception classes and catch those exception types which would precludes the need of passing any objects from the top down. Exceptions should always bubble up not down.

Abhijeet Patel
A: 

Create your own exception layer.

DALExceptionManager DuplicateException DatabaseException

BLLExceptionManager NotAuthorizedException InvalidDateException

In your Presentation Layer, Add this references and create a common exception handler. In this way you know how to deal with the exception messages.

Fleents
simple and elegant, I had something like this implemented actually. The only down side of this is extra references to include
gnomixa