This is a series of blog posts going through the great book Growing Object Oriented Software Guided By Tests, typing in code chapter by chapter, trying to add some of my own understanding where things may not be easy to grasp in the book. I highly recommand you get a copy of the book and follow along with me. Happy coding.
This post covers Chapter 19 Handling Failure, which is also the last chapter in the book related to this project.
In this chapter We add some error handling logic to the app.
What if it doesn’t work
When the sniper receives a error message from the server, We basically want to achieve the following:
- Displays the failure.
- Records the failure.
- Ignores further updates from the server.
We add a new end-to-end test: Source Code
Detecting the Failure
It is not hard to guess that when the app receives a bad message, the first place to handle it is in the
AuctionMessageTranslator. It also needs to notify the failure to its listener.
Displaying the Failure
This is also easy, when
AuctionSniper receives the
auctionFailed() callback from
AuctionMessageTranslator, it just change its snapshot’s state and notify.
Disconnecting the Sniper
XMPPAuction, add another listener that when receives
auctionFailed(), just remove the
AuctionMessageTranslator from the chat.
Recording the Failure
Filling in the Tests
We create a
ApplicationRunner using Apache Common IO library, so that we have a way to test the existance of a failure log in the system.
Failure Reporting in the Translator
We create a
XMPPFailureReporter to take the responsibility of reporting failure log, and call that when the
AuctionMessageTranslator receives a bad message.
Closing the Loop
Now it’s time to implement the
XMPPFailureReporter in the
XMPPAuctionHouse. This uses the logger in Java to log message.
OK, we are done with the Auction Sniper project for now.