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

add catchNonFatal to MonadError #1269

Merged
merged 5 commits into from
Aug 10, 2016
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
30 changes: 30 additions & 0 deletions core/src/main/scala/cats/ApplicativeError.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cats

import cats.data.{Xor, XorT}
import scala.util.{ Failure, Success, Try }
import scala.util.control.NonFatal

/**
* An applicative that also allows you to raise and or handle an error value.
Expand Down Expand Up @@ -74,6 +76,34 @@ trait ApplicativeError[F[_], E] extends Applicative[F] {
def recoverWith[A](fa: F[A])(pf: PartialFunction[E, F[A]]): F[A] =
handleErrorWith(fa)(e =>
pf applyOrElse(e, raiseError))
/**
* Often E is Throwable. Here we try to call pure or catch
* and raise.
*/
def catchNonFatal[A](a: => A)(implicit ev: Throwable <:< E): F[A] =
try pure(a)
catch {
case NonFatal(e) => raiseError(e)
}

/**
* Often E is Throwable. Here we try to call pure or catch
* and raise
*/
def catchNonFatalEval[A](a: Eval[A])(implicit ev: Throwable <:< E): F[A] =
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this just delegate to catchNonFatal with a.value ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that would be slightly slower since you would allocate another closure to do the call by name.

try pure(a.value)
catch {
case NonFatal(e) => raiseError(e)
}

/**
* If the error type is Throwable, we can convert from a scala.util.Try
*/
def fromTry[A](t: Try[A])(implicit ev: Throwable <:< E): F[A] =
t match {
case Success(a) => pure(a)
case Failure(e) => raiseError(e)
}
}

object ApplicativeError {
Expand Down
25 changes: 25 additions & 0 deletions tests/src/test/scala/cats/tests/TryTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ class TryTests extends CatsSuite {
}
}

test("catchNonFatal works") {
forAll { e: Either[String, Int] =>
val str = e.fold(identity, _.toString)
val res = MonadError[Try, Throwable].catchNonFatal(str.toInt)
// the above should just never cause an uncaught exception
// this is a somewhat bogus test:
res should not be (null)
}
}

test("catchNonFatalEval works") {
forAll { e: Either[String, Int] =>
val str = e.fold(identity, _.toString)
val res = MonadError[Try, Throwable].catchNonFatalEval(Eval.later(str.toInt))
// the above should just never cause an uncaught exception
// this is a somewhat bogus test:
res should not be (null)
}
}
test("fromTry works") {
forAll { t: Try[Int] =>
(MonadError[Try, Throwable].fromTry(t)) should === (t)
}
}

// The following tests check laws which are a different formulation of
// laws that are checked. Since these laws are more or less duplicates of
// existing laws, we don't check them for all types that have the relevant
Expand Down