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
12 changes: 11 additions & 1 deletion streams-tests/shared/src/test/scala/zio/stream/ZStreamSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3337,7 +3337,17 @@ object ZStreamSpec extends ZIOBaseSpec {
"type arguments [Error] do not conform to method refineToOrDie's type parameter bounds [E1 <: RuntimeException]"
assertM(result)(isLeft(equalTo(expected)))
} @@ scala2Only
)
),
testM("refineOrDie") {
val error = new Exception

ZStream
.fail(error)
.refineOrDie { case e: IllegalArgumentException => e }
.runDrain
.run
.map(assert(_)(dies(equalTo(error))))
}
),
suite("Constructors")(
testM("access") {
Expand Down
11 changes: 2 additions & 9 deletions streams/shared/src/main/scala/zio/stream/ZStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2276,10 +2276,7 @@ abstract class ZStream[-R, +E, +O](val process: ZManaged[R, Nothing, ZIO[R, Opti
final def refineOrDie[E1](
pf: PartialFunction[E, E1]
)(implicit ev1: E <:< Throwable, ev2: CanFail[E]): ZStream[R, E1, O] =
ZStream(self.process.map(_.mapError {
case None => None
case Some(e) if pf.isDefinedAt(e) => Some(pf.apply(e))
}))
refineOrDieWith(pf)(ev1)
Copy link
Contributor

Choose a reason for hiding this comment

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

In refineOrDieWith wouldn't it be better to use catchAll and die instead of throwing?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, yes, one sec.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in ae53e05


/**
* Keeps some of the errors, and terminates the fiber with the rest, using
Expand All @@ -2288,11 +2285,7 @@ abstract class ZStream[-R, +E, +O](val process: ZManaged[R, Nothing, ZIO[R, Opti
final def refineOrDieWith[E1](
pf: PartialFunction[E, E1]
)(f: E => Throwable)(implicit ev: CanFail[E]): ZStream[R, E1, O] =
ZStream(self.process.map(_.mapError {
case None => None
case Some(e) if pf.isDefinedAt(e) => Some(pf.apply(e))
case Some(e) => throw f(e)
}))
self.catchAll(err => (pf lift err).fold[ZStream[R, E1, O]](ZStream.die(f(err)))(ZStream.fail(_)))

/**
* Repeats the entire stream using the specified schedule. The stream will execute normally,
Expand Down