Java – Which layer should service errors be handled in

Architecturedata typesdesignjava

I am coding in a multitier architecture in Java, and I perform a query to a web service.

Is it better form for the errors returned by the service be handled in the data access layer or in the business layer?

For example, the service records a response something like this:

<?xml version="1.0" encoding="UTF-8"?>
    <ReturnCode>[integer]</ReturnCode>
    <ReturnMessage>[String correponding to ReturnCode]</ReturnMessage>
    <UpdateTimestamp>[dateTime - only present if ReturnCode == 0]</UpdateTimestamp>
    <UpdateUser>[string - only present if ReturnCode == 0]</UpdateUser>
</xml>

If I handled the errors in the data access layer, my Java code would like something like this: Error logic in DAO. (notice the data type doesn't have returnCode or returnMessage fields)

If I handled the errors in the business layer, the code would look like this: Error logic in BO. (notice the data type has returnCode and returnMessage fields)

Best Answer

I'd say that Fail-fast approach is also applicable in your situation. That is, you should handle the error as close to its source as you can. If you do this webservice call from the business layer and then want to save results of that call in the database, then you should handle error in the business layer. If it's done as a part of data access layer, handle it there.

Related Topic