[dry-operation] on_failure handler's first argument

Hello, I’m trying to implement #on_failure handler for my operation and I was expecting the first argument to be a monad, instead I’m getting unwrapped result passed to Failure.

def on_failure(failure, step_name)
  @failure_handler.call(failure:, step_name:)
end

Inside the handler I wanted to do pattern matching on the failure and apply proper behavior. But IDK if should expect the Failure or its value.

Also I have some doubts about the second argument as from what I noticed it always returns :call, no matter which step actually failed.

Hey @buszu, yeah, failure is the unwrapped result and not the actual Monad. This is because that won’t ever be a Success, so you shouldn’t need to pattern match based on that condition. Does it make sense?

About the second argument, it turns out docs are wrong and the method received is the one wrapping the steps, which is :call by default (I’ll fix the docs). This is because you can have different operations defined in a single class (e.g. by using operate_on :call, :run). If you want to reference a particular step, you should include it in the unwrapped result you give to Failure.

Fixing docs: Fix docs on error handling by waiting-for-dev · Pull Request #32 · dry-rb/dry-operation · GitHub

I think it makes sense, anyway I can still match arrays or simply ‚failure.first’ with normal case assuming that I return failures’ values as arrays where the first element is a symbol describing the failure.

Also this pattern should be enough to determine where the failure happened, so probably I don’t need to use the second argument of the error handler.

Thank you for your reply and fixing the documentation!

1 Like