Part 4 of 6 · 1 min read
Falling off the end
No guarantee of handling.
GoF names the main liability. A request has no named receiver, so nothing promises that anyone will handle it. It can run off the end of the chain, or a chain that was wired wrongly can leave it unhandled. In the example, 250000 gets a clear answer because handle checks for a missing successor. Without that check, the request would silently disappear, or throw a null pointer error.
Ways to deal with it:
- End the chain with a default handler that always answers, for example by rejecting the request with a reason.
- Return a result that says whether the request was handled, and make the caller deal with "no".
- Test the full chain with one request for every case, including a request no handler can take.
Another design choice is whether the chain stops at the first handler that acts, as above, or lets every handler act. Approval stops at the first. Logging filters may let every handler see the request. Be clear which one you mean, because the two behave differently when you add a handler.
Check yourself: 1. What happens in the example to a request for 250000, and why? 2. Name two ways to handle a request that falls off the end.