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 1 commit
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
20 changes: 20 additions & 0 deletions core/src/main/scala/cats/ApplicativeError.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats

import cats.data.{Xor, XorT}
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 +75,25 @@ 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 tryCatch[A](a: => A)(implicit ev: Throwable =:= E): F[A] =
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about naming this catchNonFatal to match the method on Xor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call. Updated.

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

/**
* Often E is Throwable. Here we try to call pure or catch
* and raise
*/
def tryCatchEval[A](a: Eval[A])(implicit ev: Throwable =:= E): F[A] =
try pure(a.value)
catch {
case NonFatal(e) => raiseError(e)
}
}

object ApplicativeError {
Expand Down
20 changes: 20 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,26 @@ class TryTests extends CatsSuite {
}
}

test("tryCatch works") {
forAll { e: Either[String, Int] =>
val str = e.fold(identity, _.toString)
val res = MonadError[Try, Throwable].tryCatch(str.toInt)
// the above shuold just never cause an uncaught exception
Copy link
Contributor

Choose a reason for hiding this comment

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

s/shuold/should

// this is a somewhat bogus test:
res should not be (null)
}
}

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

// 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