Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2169,6 +2169,13 @@ object ZStreamSpec extends ZIOBaseSpec {
}
}
},
testM("onError") {
for {
flag <- Ref.make(false)
exit <- ZStream.fail("Boom").onError(_ => flag.set(true)).runDrain.run
called <- flag.get
} yield assert(called)(isTrue) && assert(exit)(fails(equalTo("Boom")))
} @@ zioTag(errors),
testM("orElse") {
val s1 = ZStream(1, 2, 3) ++ ZStream.fail("Boom")
val s2 = ZStream(4, 5, 6)
Expand Down
8 changes: 8 additions & 0 deletions streams/shared/src/main/scala/zio/stream/ZStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,14 @@ abstract class ZStream[-R, +E, +O](val process: ZManaged[R, Nothing, ZIO[R, Opti
}
}

/**
* Runs the specified effect if this stream fails, providing the error to the effect if it exists.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The corresponding method on ZIO guarantees the execution of cleanup even in the face of interruption, and also executes it uninterruptibly.

I don't think it's critical to replicate those same semantics here, but let's add a note on the scaladoc about this difference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm not sure if it's possible to provide the same semantics.
I added a note to the scaladoc.

*
* Note: Unlike [[ZIO.onError]], there is no guarantee that the provided effect will not be interrupted.
*/
final def onError[R1 <: R](cleanup: Cause[E] => URIO[R1, Any]): ZStream[R1, E, O] =
catchAllCause(cause => ZStream.fromEffect(cleanup(cause) *> ZIO.halt(cause)))

/**
* Switches to the provided stream in case this one fails with a typed error.
*
Expand Down