New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do-notation: wrapping method with global rescue works incorrect #72
Comments
Basically, wildcard rescue is an anti-pattern so it's no surprise you run into issues with it. |
I understand that it is an anti-pattern example. In my example wildcard rescue is used in each service object to call notification service, this logic can be moved to controller, but I like it more in services. |
Ugh, nope, not gonna work. People may depend on the current behavior in situations like this def call
transaction do
yield op1
yield op2
end
end where def transaction
begin_transaction
yield
commit_transaction
rescue => e
roll_back_transaction
raise e
end What I had in mind is changing |
If you want to have this behavior across all your objects you may want to consider wrapping them on registration within a container, i.e. every object can be wrapped with a proc: wrapped = proc do |*args|
obj.(*args)
rescue => e
# ...
Failure(e)
end |
Ok, got it. Another approach is to rescue class Documents::CreateDocument
def call
# ...
rescue Dry::Monads::Do::Halt => e
e.result
rescue => e
GlobalContainer['support.services.exception_notifier'].call(e)
Failure(e)
end
end |
Do-notation in method that has a global rescue works incorrectly. Here is an example.
Lets suggest that
validate
is a method that returnsDry::Monads::Failure
for incorrect input.I expected to receive
Failure
with result of#validate
method, instead of it I receivedFailure
withDry::Monads::Do::Halt
. This happens because https://github.com/dry-rb/dry-monads/blob/master/lib/dry/monads/do.rb#L137 is not called whenHalt
is raised here https://github.com/dry-rb/dry-monads/blob/master/lib/dry/monads/do.rb#L132.Halt
is captured by globalrescue
is original method.From my point of view this can be fixed by aliasing and calling original method instead of overriding it.
The text was updated successfully, but these errors were encountered: