-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call a global #on_failure hook when the flow fails
Most of the time, individual operations are responsible for handling their own failures. However, there are cases when it makes sense to handle failures globally, for example, when you want to log the error for a given flow. When using the raw behavior, i.e., when we don't prepend around methods, that can be easily handled manually: ```ruby class CreateUser < Dry::Operation skip_prepending def call(input) steps do attrs = step validate(input) step persist(attrs) user end.tap do |result| log_failure(result.failure) if result.failure? end end # ... end ``` However, by automatically wrapping around `#steps` we gain focus on the happy path, but we lose the ability to handle global failures. Because of that, we introduce an `#on_failure` hook that is only called when using the prepended behavior on a failing flow. The method accepts the unwrapped failure extracted from the result object. ```ruby class CreateUser < Dry::Operation def call(input) attrs = step validate(input) step persist(attrs) user end private def on_failure(failure) log_failure(failure) end # ... end ``` `#on_failure` can also take a second optional argument, which will be assigned to the prepended method's name. That's useful when we're defining more than one flow in a single class. ```ruby class UserFlows < Dry::Operation operate_on :create_user, :delete_user # ... private def on_failure(failure, flow_name) case flow_name when :create_user # ... when :delete_user # ... end end # ... end ``` The calling of the hook is done via an injected result handler lambda. At some point, we might want to make this behavior configurable.
- Loading branch information
1 parent
eac1bd5
commit 0a71b69
Showing
4 changed files
with
217 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters