Skip to content

Commit

Permalink
Merge pull request #341 from Kevin-Lee/task/303/make-Fx-CanRecover
Browse files Browse the repository at this point in the history
Close #303 - Make Fx CanRecover
  • Loading branch information
kevin-lee committed Jan 2, 2022
2 parents e7bd81e + 5590f9d commit 624beb3
Show file tree
Hide file tree
Showing 26 changed files with 13,642 additions and 3,798 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ lazy val props =
final val Scala3Versions = List("3.0.0")
final val Scala3Version = Scala3Versions.head

// final val ProjectScalaVersion = Scala2Version
final val ProjectScalaVersion = Scala3Version
final val ProjectScalaVersion = Scala2Version
// final val ProjectScalaVersion = Scala3Version

lazy val licenses = List("MIT" -> url("http://opensource.org/licenses/MIT"))

Expand Down
8 changes: 4 additions & 4 deletions cats-effect/src/main/scala-2/effectie/cats/CanRecover.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ object CanRecover {

implicit object IoCanRecover extends CanRecover[IO] {

override def recoverFromNonFatalWith[A, AA >: A](fa: => IO[A])(
@inline override final def recoverFromNonFatalWith[A, AA >: A](fa: => IO[A])(
handleError: PartialFunction[Throwable, IO[AA]]
): IO[AA] =
fa.handleErrorWith(err => handleError.applyOrElse(err, ApplicativeError[IO, Throwable].raiseError[AA]))

override def recoverFromNonFatal[A, AA >: A](fa: => IO[A])(handleError: PartialFunction[Throwable, AA]): IO[AA] =
@inline override final def recoverFromNonFatal[A, AA >: A](fa: => IO[A])(handleError: PartialFunction[Throwable, AA]): IO[AA] =
recoverFromNonFatalWith[A, AA](fa)(handleError.andThen(IO.pure(_)))

}

implicit object IdCanRecover extends CanRecover[Id] {

@SuppressWarnings(Array("org.wartremover.warts.Throw"))
override def recoverFromNonFatalWith[A, AA >: A](fa: => Id[A])(
@inline override final def recoverFromNonFatalWith[A, AA >: A](fa: => Id[A])(
handleError: PartialFunction[Throwable, Id[AA]]
): Id[AA] =
try (fa)
Expand All @@ -38,7 +38,7 @@ object CanRecover {
throw ex
}

override def recoverFromNonFatal[A, AA >: A](fa: => Id[A])(
@inline override final def recoverFromNonFatal[A, AA >: A](fa: => Id[A])(
handleError: PartialFunction[Throwable, AA]
): Id[AA] =
recoverFromNonFatalWith[A, AA](fa)(handleError)
Expand Down
11 changes: 11 additions & 0 deletions cats-effect/src/main/scala-2/effectie/cats/Fx.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ object Fx {

@inline override final def handleNonFatal[A, AA >: A](fa: => IO[A])(handleError: Throwable => AA): IO[AA] =
CanHandleError.IoCanHandleError.handleNonFatal[A, AA](fa)(handleError)

@inline override final def recoverFromNonFatalWith[A, AA >: A](fa: => IO[A])(handleError: PartialFunction[Throwable, IO[AA]]): IO[AA] =
CanRecover.IoCanRecover.recoverFromNonFatalWith[A, AA](fa)(handleError)

@inline override final def recoverFromNonFatal[A, AA >: A](fa: => IO[A])(handleError: PartialFunction[Throwable, AA]): IO[AA] =
CanRecover.IoCanRecover.recoverFromNonFatal[A, AA](fa)(handleError)
}

implicit object IdFx extends Fx[Id] {
Expand All @@ -49,6 +55,11 @@ object Fx {
@inline override final def handleNonFatal[A, AA >: A](fa: => Id[A])(handleError: Throwable => AA): Id[AA] =
CanHandleError.IdCanHandleError.handleNonFatal[A, AA](fa)(handleError)

@inline override final def recoverFromNonFatalWith[A, AA >: A](fa: => Id[A])(handleError: PartialFunction[Throwable, Id[AA]]): Id[AA] =
CanRecover.IdCanRecover.recoverFromNonFatalWith[A, AA](fa)(handleError)

@inline override final def recoverFromNonFatal[A, AA >: A](fa: => Id[A])(handleError: PartialFunction[Throwable, AA]): Id[AA] =
CanRecover.IdCanRecover.recoverFromNonFatal[A, AA](fa)(handleError)
}

}
3 changes: 2 additions & 1 deletion cats-effect/src/main/scala-3/effectie/cats/CanRecover.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cats.*
import cats.data.EitherT
import cats.effect.*

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal

Expand All @@ -26,7 +27,7 @@ object CanRecover {
}

given futureCanRecover(using ec: ExecutionContext): CanRecover[Future] =
new effectie.CanRecover.FutureCanRecover(ec) with CanRecover[Future]
new effectie.CanRecover.CanRecoverFuture with CanRecover[Future]

given idCanRecover: CanRecover[Id] with {

Expand Down
12 changes: 12 additions & 0 deletions cats-effect/src/main/scala-3/effectie/cats/Fx.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ object Fx {
inline override final def handleNonFatal[A, AA >: A](fa: => IO[A])(handleError: Throwable => AA): IO[AA] =
CanHandleError.ioCanHandleError.handleNonFatal(fa)(handleError)

inline override final def recoverFromNonFatalWith[A, AA >: A](fa: => IO[A])(handleError: PartialFunction[Throwable, IO[AA]]): IO[AA] =
CanRecover.ioCanRecover.recoverFromNonFatalWith(fa)(handleError)

inline override final def recoverFromNonFatal[A, AA >: A](fa: => IO[A])(handleError: PartialFunction[Throwable, AA]): IO[AA] =
CanRecover.ioCanRecover.recoverFromNonFatal(fa)(handleError)

}

given idFx: Fx[Id] with {
Expand All @@ -52,6 +58,12 @@ object Fx {
inline override final def handleNonFatal[A, AA >: A](fa: => Id[A])(handleError: Throwable => AA): Id[AA] =
CanHandleError.idCanHandleError.handleNonFatal(fa)(handleError)

inline override final def recoverFromNonFatalWith[A, AA >: A](fa: => Id[A])(handleError: PartialFunction[Throwable, Id[AA]]): Id[AA] =
CanRecover.idCanRecover.recoverFromNonFatalWith(fa)(handleError)

inline override final def recoverFromNonFatal[A, AA >: A](fa: => Id[A])(handleError: PartialFunction[Throwable, AA]): Id[AA] =
CanRecover.idCanRecover.recoverFromNonFatal(fa)(handleError)

}

}
3 changes: 0 additions & 3 deletions cats-effect/src/main/scala-3/effectie/cats/FxCtor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ object FxCtor {

}

given futureFxCtor(using EC: ExecutionContext): FxCtor[Future] =
effectie.Fx.fxFuture

given idFxCtor: FxCtor[Id] with {

inline override final def effectOf[A](a: => A): Id[A] = a
Expand Down

0 comments on commit 624beb3

Please sign in to comment.