Which layer should service errors be handled in?

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?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>
</code>
<code><?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> </code>
<?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)

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.

The underlying layers — service / DAO in your parlance — should just throw exceptions when they hit them. Let the UI layer (web services in this scenario) handle how to deal with it for the client.

The main issue is if you try and handle it down low then your service / dao need to have more intimate knowlege of the top end of the stack then they really should leading to a whole host of other design issues. Being able to fire and forget exceptions like this is what makes them much more powerful than other error management techniques.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật