Solved

Java Message handling in Service Layer

Posted on 2016-10-13
3
38 Views
Last Modified: 2016-10-18
I´m designing a service layer in a multilayerd architecture (Service, Business, Dao). Suposse I need a method like getUserAccounts

This method just return a list of Accounts, but what if my dao layer can´t connect to database, or if I have some error when reading the recorset, a parse problem for example. Or maybe another error in the business layer.

Do I have to catch the exception in the service layer, and map to a error code?
Do I have to catch the exception in the business layer, and the buisness layer return a bean with the error code? Avoiding to have any kind of logic in the service layer, more than exposing the Rest API
0
Comment
Question by:anonymous
3 Comments
 
LVL 15

Accepted Solution

by:
gurpsbassi earned 500 total points
ID: 41842543
Throw an exception and handle it in your rest layer with a exception handler.
You don't need to map all the error codes as you described. A generic error message is sufficient.
No user of your application would be interested to know the update failed because it violated referential integrity.
1
 

Author Comment

by:anonymous
ID: 41842563
Great! If the exception is in the DAO Layer should I catch it in the Business layer also? or only in the service layer?
0
 
LVL 26

Expert Comment

by:dpearson
ID: 41842935
You should always catch Exceptions at the level of the system that knows how to correctly handle that exception.

In other words, "it depends".  If you know what to do at the business layer to handle the exception (e.g. database offline, let's check the cache) then handle it there.

But if you need to return to the user "it didn't work" you should let it bubble up to the service layer and send the error out to the user.

Doug
1

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If, like me, you find yourself repeatedly and tediously joining many segments (lines, arcs) in other people's drawings back into polylines that can be used more effectively in Computer Aided Machining and Laser Cutting, then this article is for you!…
In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now