Skip to content

Commit

Permalink
Implement handleErrorWith with MonadCancelThrow
Browse files Browse the repository at this point in the history
Deprecated handleErrorWith with ApplicativeError
  • Loading branch information
lenguyenthanh committed Sep 5, 2024
1 parent 2839446 commit 3c2d6af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private[effect] object JvmCpuStarvationMetrics {
case (mbeanServer, _) => IO.blocking(mbeanServer.unregisterMBean(mBeanObjectName))
}
.map(_._2)
.handleErrorWith[CpuStarvationMetrics, Throwable] { th =>
.handleErrorWith[CpuStarvationMetrics] { th =>
Resource.eval(Console[IO].errorln(warning(th))).map(_ => new NoOpCpuStarvationMetrics)
}
}
Expand Down
10 changes: 9 additions & 1 deletion kernel/shared/src/main/scala/cats/effect/kernel/Resource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,16 @@ sealed abstract class Resource[F[_], +A] extends Serializable {
}
}

@deprecated("Use overload with MonadCancelThrow", "3.6.0")
def handleErrorWith[B >: A, E](f: E => Resource[F, B])(
implicit F: ApplicativeError[F, E]): Resource[F, B] =
F: ApplicativeError[F, E]): Resource[F, B] =
attempt(F).flatMap {
case Right(a) => Resource.pure(a)
case Left(e) => f(e)
}

def handleErrorWith[B >: A](f: Throwable => Resource[F, B])(
implicit F: MonadCancelThrow[F]): Resource[F, B] =
attempt(F).flatMap {
case Right(a) => Resource.pure(a)
case Left(e) => f(e)
Expand Down

0 comments on commit 3c2d6af

Please sign in to comment.