https://kotlinlang.org logo
#arrow
Title
# arrow
m

Mervyn McCreight

03/06/2024, 4:07 PM
Hey 🙂 I have a question about the saga-pattern implementation of arrow (https://arrow-kt.io/learn/resilience/saga/). Does it only work for exceptions, or can a saga action also use the raise-dsl or other forms or typed error handling? Looking at the implementation (https://github.com/arrow-kt/arrow/blob/main/arrow-libs/resilience/arrow-resilience/src/commonMain/kotlin/arrow/resilience/Saga.kt) I’d assume it only works for actions throwing exceptions in case of a failure, but I may also be wrong 🙂
s

simon.vergauwen

03/06/2024, 5:24 PM
It works with Raise! All DSL from Arrow do! Including cancellation on Arrow Fx Coroutines, Saga, Resource, AutoClose, etc. Small example:
Copy code
either {
  saga {
    saga({ println("Action A") }) { println("Rollback A") }
    raise("failed")
    1
  }.transact()
}
There is however a big difference between
either { saga { } }
or
saga { either { } }
. The first is a computation that can fail with
E
, which inside is using a
saga
. Since
Saga
sees
raise
as a failure, it will rollback action A. In the latter case, we have a
Saga
that computes an
Either
. So any
Left.bind
, or
raise
, that is encountered will not fail the
Saga
but would return
Either.Left
if
transact
is called on the
Saga
. Hope that helps! I think we should probably add this example to the documentation ☺️ I find it really neat that all DSLs can be nested, and that depending on the order you can get different behavior.
👍 1
m

Mervyn McCreight

03/06/2024, 5:28 PM
Thanks for the detailed explanation! IMO it would be helpful to explain that in the documentation as well, but it might also be just me 🙂 > I find it really neat that all DSLs can be nested, and that depending on the order you can get different behavior. 👍 - similar to how it is with
parMap, ...
s

simon.vergauwen

03/06/2024, 5:31 PM
Yes, I've been meaning to add it website for a while.. 😕
m

Mervyn McCreight

03/07/2024, 2:59 PM
FYI: It works like a charm and I love it 😄
🙌 1
s

simon.vergauwen

03/07/2024, 3:11 PM
Awesome, happy to hear! arrow intensifies
9 Views