Rubyists! In the process of doing an "extract method object" refactoring, I wound up with a service object that calls several other (inner) service objects. A simplified version is below.
Note that (a) I'm using a simple Result object to report success/failure back to the calling code, and (b) I'm passing the same Result instance around to subservices (to keep track of overall process state).
My question is this: in a multistep process, how should I handle failure in an intermediate step?
The options I can think of are:
- As in the example below (
do_phase_3 unless @result.failure?
), explicitly check for failure in a substep, and abort manually. - Raise an exception, which gets caught in the outer (class-level) .perform call, which then returns the result object as usual.
- Use throw/catch to accomplish the same effect as #2, above.
Am I missing other options? Which one would be best?
Highly dependent on context. I actually wrote a whole framework once to raise the level of abstraction on stuff like this where failures in different steps might need to be handled differently. Sadly, it is not really documented. https://github.com/avdi/methodical