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

ZValidation: add toEitherException, toFuture and make toTry not discard errors #803

Closed
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
47 changes: 41 additions & 6 deletions core/shared/src/main/scala/zio/prelude/ZValidation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ sealed trait ZValidation[+W, +E, +A] { self =>
final def toEither: Either[NonEmptyChunk[E], A] =
fold(Left(_), Right(_))

/**
* 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[Failure.Exception[W, E], A] = self match {
case Success(_, value) => Right(value)
case failure @ Failure(_, _) => Left(failure.toException)
}

/**
* Transforms this `ZValidation` to an `Either`, discarding the order in which the errors occurred and discarding the log.
*/
Expand All @@ -230,11 +238,20 @@ 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 `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(_, _) => scala.concurrent.Future.failed(failure.toException)
}

/**
* 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(_, _) => scala.util.Failure(failure.toException)
}

/**
* Converts this `ZValidation` into a `ZIO` effect, discarding the log.
Expand Down Expand Up @@ -287,9 +304,27 @@ sealed trait ZValidation[+W, +E, +A] { self =>
object ZValidation extends LowPriorityValidationImplicits {

final case class Failure[+W, +E](log: Chunk[W], errors: NonEmptyChunk[E]) extends ZValidation[W, E, Nothing] {

/** The errors represented in a way which discards the order in which they occurred. */
lazy val errorsUnordered: NonEmptyMultiSet[E] = NonEmptyMultiSet.fromIterable(errors.head, errors.tail)

/** The description of the failure. */
lazy val message: String =
s"errors:\n ${errors.map(_.toString).mkString("\n ")}\nlog:\n ${log.map(_.toString).mkString("\n ")}"

/** The same failure, but represented as an `Exception` which also has the `cause` set to the first error that occurred */
def toException(implicit ev: E <:< Throwable): Failure.Exception[W, E] = Failure.Exception(this)
}
final case class Success[+W, +A](log: Chunk[W], value: A) extends ZValidation[W, Nothing, A]

object Failure {
final case class Exception[+W, +E](failure: Failure[W, E])(implicit ev: E <:< Throwable)
Copy link
Contributor

Choose a reason for hiding this comment

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

We're exposing a lot of implementations details here and we are also potentially creating some inaccurate information. The returned Throwable is a subtype of RuntimeException even though it is possible that none of the original throwable are subtypes of RuntimeException. I would suggest we just take the first error and then add the others as suppressed exceptions to that, discarding the log. There are a variety of other operators that users can use to get the log if they want.

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean to mutate an existing Exception by adding others as suppressed? Wouldn't that be a bit misleading? How could users recognize the original suppressed exceptions in that particular Exception from those that we would add?

Or do you think it would be better to use a mere Exception instead of RuntimeException.

I don't have a strong opinion on this, so let me know, if you still think adding rest as suppressed to the first one is the best way to go.

extends scala.Exception(failure.message) {
failure.errors.tail.foreach(addSuppressed(_))
initCause(failure.errors.head)
}
}

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

/**
* The `Covariant` instance for `ZValidation`.
Expand All @@ -301,7 +336,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