Last active
February 15, 2020 13:10
-
-
Save dnaumenko/d7b1b1f1a035117b8bb25b14c6216abf to your computer and use it in GitHub Desktop.
Convert from ZIO exit to your own exit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Exit[+A] { | |
def toEither: Either[Throwable, A] | |
} | |
object Exit { | |
final case class Success[+A](value: A) extends Exit[A] { | |
override def toEither: Either[Throwable, A] = Right(value) | |
} | |
sealed trait Failure extends Exit[Nothing] | |
final case class Error[+E <: Throwable](error: E) extends Exit.Failure { | |
override def toEither: Either[Throwable, Nothing] = Left(error) | |
} | |
final case class Termination(error: Throwable) extends Exit.Failure { | |
override def toEither: Either[Throwable, Nothing] = Left(error) | |
} | |
def fromZIOExit[E <: Throwable, A](result: zio.Exit[E, A]): Exit[A] = result match { | |
case zio.Exit.Success(v) => Success(v) | |
case zio.Exit.Failure(failure) => fromZIOFailure(failure) | |
} | |
// Impl borrowed from https://github.com/7mind/izumi/blob/develop/fundamentals/fundamentals-bio/src/main/scala/izumi/functional/bio/BIOExit.scala | |
def fromZIOFailure[E <: Throwable](failure: zio.Cause[E]): Failure = | |
failure.failureOrCause match { | |
case Left(err) => Error(err) | |
case Right(cause) => | |
val unchecked = cause.defects | |
val exceptions = if (cause.interrupted) { | |
new InterruptedException :: unchecked | |
} else { | |
unchecked | |
} | |
val compound = exceptions match { | |
case e :: Nil => e | |
case _ => FiberFailure(cause) | |
} | |
Termination(compound) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment