Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable converting ZValidation.Failure to an exception #1360

Merged
merged 6 commits into from
Aug 15, 2024
Merged
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
37 changes: 31 additions & 6 deletions core/shared/src/main/scala/zio/prelude/ZValidation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ sealed trait ZValidation[+W, +E, +A] { self =>
case Success(_, value) => Right(value)
}

/**
* Transforms `ZValidation` to an `Either`, where the failure case will be represented as an `Exception` with the first error as `cause`.
*/
final def toEitherException(implicit ev: E <:< Throwable): Either[ZValidationException[W, E], A] = self match {
case Success(_, value) => Right(value)
case failure: Failure[W, E] => Left(failure.toException)
}

/**
* Transforms this `ZValidation` to an `Either`, discarding the order in which the errors occurred and discarding the log.
*/
Expand All @@ -264,6 +272,14 @@ sealed trait ZValidation[+W, +E, +A] { self =>
final def toEitherWith[E2](f: NonEmptyChunk[E] => E2): Either[E2, A] =
toEither.left.map(f)

/**
* Transforms this `ZValidation` to a `Future`, discarding the log.
*/
final def toFuture(implicit ev: E <:< Throwable): scala.concurrent.Future[A] = self match {
case Success(_, value) => scala.concurrent.Future.successful(value)
case failure: Failure[W, E] => scala.concurrent.Future.failed(failure.toException)
}

/**
* Transforms this `ZValidation` to an `Option`, discarding information about
* the errors and log.
Expand All @@ -272,11 +288,12 @@ sealed trait ZValidation[+W, +E, +A] { self =>
fold(_ => None, Some(_))

/**
* Transforms this `ZValidation` to a `Try`, discarding all but the first
* error and the log.
* Transforms this `ZValidation` to a `Try`, discarding the log.
*/
final def toTry(implicit ev: E <:< Throwable): scala.util.Try[A] =
fold(es => scala.util.Failure(ev(es.head)), scala.util.Success(_))
final def toTry(implicit ev: E <:< Throwable): scala.util.Try[A] = self match {
case Success(_, value) => scala.util.Success(value)
case failure: Failure[W, E] => scala.util.Failure(failure.toException)
}

/**
* Converts this `ZValidation` into a `ZIO` effect, discarding the log.
Expand Down Expand Up @@ -356,8 +373,16 @@ object ZValidation extends LowPriorityValidationImplicits {

final case class Failure[+W, +E](log: Chunk[W], errors: NonEmptyChunk[E]) extends ZValidation[W, E, Nothing] {
lazy val errorsUnordered: NonEmptyMultiSet[E] = NonEmptyMultiSet.fromIterable(errors.head, errors.tail)

def toException(implicit ev: E <:< Throwable): ZValidationException[W, E] = ZValidationException(this)
}
final case class Success[+W, +A](log: Chunk[W], value: A) extends ZValidation[W, Nothing, A]

final case class ZValidationException[+W, +E](failure: Failure[W, E])(implicit ev: E <:< Throwable)
extends RuntimeException(ev(failure.errors.head)) {
failure.errors.tail.foreach(addSuppressed(_))
}

final case class Success[+W, +A](log: Chunk[W], value: A) extends ZValidation[W, Nothing, A]

/**
* The `Covariant` instance for `ZValidation`.
Expand All @@ -369,7 +394,7 @@ object ZValidation extends LowPriorityValidationImplicits {
}

/**
* Derives a `Debug[ZValidation[W, E, A]]` given a `Debug[W], a `Debug[E]`,
* Derives a `Debug[ZValidation[W, E, A]]` given a `Debug[W]`, a `Debug[E]`,
* and a `Debug[A]`.
*/
implicit def ZValidationDebug[W: Debug, E: Debug, A: Debug]: Debug[ZValidation[W, E, A]] = {
Expand Down