Software Programmer
asked on
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Can you help me with a code snippet ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Still not clear. May be a template of codes in all layers will be helpful
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.
ASKER
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.
I posted some sample code here.
Both are valid comments.
ASKER