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
16 changes: 16 additions & 0 deletions core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,22 @@ object ZIOSpec extends ZIOBaseSpec {
assertCompletes
}
),
suite("fromEither") {
test("produces a stack trace on failure") {
ZIO
.fromEither(Left("foo"))
.catchAllCause(cause => ZIO.succeed(cause.trace.stackTrace))
.map(t => assertTrue(t.nonEmpty))
}
},
suite("fromEitherCause") {
test("produces a stack trace on failure") {
ZIO
.fromEitherCause(Left(Cause.fail("foo")))
.catchAllCause(cause => ZIO.succeed(cause.trace.stackTrace))
.map(t => assertTrue(t.nonEmpty))
}
},
suite("fromFutureInterrupt")(
test("running Future can be interrupted") {
import java.util.concurrent.atomic.AtomicInteger
Expand Down
14 changes: 12 additions & 2 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3708,13 +3708,23 @@ object ZIO extends ZIOCompanionPlatformSpecific with ZIOCompanionVersionSpecific
* Lifts an `Either` into a `ZIO` value.
*/
def fromEither[E, A](v: => Either[E, A])(implicit trace: Trace): IO[E, A] =
succeed(v).flatMap(_.fold(ZIO.failFn, ZIO.successFn))
ZIO.suspendSucceed {
v match {
case Right(s) => Exit.succeed(s)
case Left(e) => ZIO.fail(e)
}
}

/**
* Lifts an `Either` into a `ZIO` value.
*/
def fromEitherCause[E, A](v: => Either[Cause[E], A])(implicit trace: Trace): IO[E, A] =
succeed(v).flatMap(_.fold(Exit.failCause, ZIO.successFn))
ZIO.suspendSucceed {
v match {
case Right(s) => Exit.succeed(s)
case Left(c) => ZIO.failCause(c)
}
}

/**
* Creates a `ZIO` value that represents the exit value of the specified
Expand Down
Loading