# \[dry-operation\] on\_failure handler's first argument

**URL:** <https://discourse.dry-rb.org/t/dry-operation-on-failure-handlers-first-argument/1913>\
**Category:** Uncategorized\
**Created:** [March 22, 2025, 11:32am UTC](https://discourse.dry-rb.org/t/dry-operation-on-failure-handlers-first-argument/1913 "2025-03-22T11:32:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![buszu](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/buszu/32/615_2.png) [@buszu](https://discourse.dry-rb.org/u/buszu)\
**Post date:** [March 22, 2025, 11:32am UTC](https://discourse.dry-rb.org/t/dry-operation-on-failure-handlers-first-argument/1913/1 "2025-03-22T11:32:00Z")

</div>

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.

```ruby
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.

---

<div class="post-metadata">

**Author:** ![buszu](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/buszu/32/615_2.png) [@buszu](https://discourse.dry-rb.org/u/buszu)\
**Post date:** [March 24, 2025, 9:11am UTC](https://discourse.dry-rb.org/t/dry-operation-on-failure-handlers-first-argument/1913/2 "2025-03-24T09:11:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![waiting-for-dev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/waiting-for-dev/32/71_2.png) [@waiting-for-dev](https://discourse.dry-rb.org/u/waiting-for-dev)\
**Post date:** [March 25, 2025, 3:11pm UTC](https://discourse.dry-rb.org/t/dry-operation-on-failure-handlers-first-argument/1913/3 "2025-03-25T15:11:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![waiting-for-dev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/waiting-for-dev/32/71_2.png) [@waiting-for-dev](https://discourse.dry-rb.org/u/waiting-for-dev)\
**Post date:** [March 25, 2025, 3:15pm UTC](https://discourse.dry-rb.org/t/dry-operation-on-failure-handlers-first-argument/1913/4 "2025-03-25T15:15:54Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![waiting-for-dev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/waiting-for-dev/32/71_2.png) [@waiting-for-dev](https://discourse.dry-rb.org/u/waiting-for-dev)\
**Post date:** [March 25, 2025, 3:20pm UTC](https://discourse.dry-rb.org/t/dry-operation-on-failure-handlers-first-argument/1913/5 "2025-03-25T15:20:23Z")

</div>

Fixing docs: [Fix docs on error handling by waiting-for-dev · Pull Request #32 · dry-rb/dry-operation · GitHub](https://github.com/dry-rb/dry-operation/pull/32)

---

<div class="post-metadata">

**Author:** ![buszu](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/buszu/32/615_2.png) [@buszu](https://discourse.dry-rb.org/u/buszu)\
**Post date:** [March 25, 2025, 5:42pm UTC](https://discourse.dry-rb.org/t/dry-operation-on-failure-handlers-first-argument/1913/6 "2025-03-25T17:42:04Z")

</div>

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!
