Exception Handling in Controller

We are following MVC pattern - model view controller and flow is

Controller -> Service -> Dao -> JdbcTemplate -> MySQL

1. All the errors should be cached and thrown in controller layer ? (OR) Error should be thrown from the controller method and it should be handled in the front end - js or jsp files ?

2. Do we need to catch DAO exception in the service layer or just pass the thrown exception to the controller layer and let the controller handle the exception?

3. Customized exception can be thrown on DAO and in service layer. However does the final exception handling should be done in controller side or in service layer.

4. Does the controller should catch any exception and create the localized error message and just pass the error message as string and response type as error to the front end to handle the error in the front end ?

Thanks.
Software ProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
There is no "should" or "must". There are several paths to a destination.

1) Why do errors need to be cached? Errors can be thrown from the controller or the controller can simply return an error object (not throw an exception).

2) This is up to you. I usually catch DAO exceptions in the DAO layer and return a status code to the controller.

3) Again, this is up to you. Handle exceptions on DAO layer and return it to the controller or throw them in DAO and let the controller catch them. It is entirely up to you.

4)
create the localized error message and just pass the error message as string and response type as error to the front end to handle the error in the front end ?

This is what I usually do.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Software ProgrammerAuthor Commented:
Do you say different Object on success and different object on Error ???
0
girionisCommented:
No, one object that is also composed of an error object. If there are errors then the error object is filled, otherwise it is empty/null.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Software ProgrammerAuthor Commented:
Can you help me with a code snippet ?
0
girionisCommented:
Something like this

public class CustomerResponseWrapper {
    Customer customer;
    Error error;
}

Open in new window

0
Software ProgrammerAuthor Commented:
Still not clear. May be a template of codes in all layers will be helpful
0
girionisCommented:
Sorry I cannot post production code here. The thought is that you have a Java object (a wrapper) which contains both the actual response (lets say a Customer in my example) and an error response. Then you return this object to the client every time. If the error of the wrapper has value/is not null then it means that there was an error. By doing this you always return a 200 status code and the only thing the client has to do is to check if the error is empty/null.
0
Software ProgrammerAuthor Commented:
In general, couple of code snippet would be helpful to understand the solution and implement it. It would be hard to understand without sample code snippet or template.
0
girionisCommented:
I posted some sample code here.
0
girionisCommented:
Both are valid comments.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.