Skip to content

Commit

Permalink
Add reportError helper and use it for onError
Browse files Browse the repository at this point in the history
With this change, if the onError callback raises any error, We'll
report that error and then re-raise the original error.
  • Loading branch information
lenguyenthanh committed Aug 22, 2024
1 parent f4f505c commit 40a7dde
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {
* Implements `ApplicativeError.onError`.
*/
def onError(pf: PartialFunction[Throwable, IO[Unit]]): IO[A] =
handleErrorWith(t => pf.applyOrElse(t, (_: Throwable) => IO.unit) *> IO.raiseError(t))
handleErrorWith(t =>
pf.applyOrElse(t, (_: Throwable) => IO.unit).reportError *> IO.raiseError(t))

/**
* Like `Parallel.parProductL`
Expand Down Expand Up @@ -941,6 +942,19 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {
def void: IO[Unit] =
map(_ => ())

/**
* Similar to [[IO.voidError]], but also reports the error.
*/
private[effect] def reportError(implicit ev: A <:< Unit): IO[Unit] = {
val _ = ev
asInstanceOf[IO[Unit]].handleErrorWith { t =>
IO.executionContext.flatMap(ec => IO(ec.reportFailure(t)))
}
}

/**
* Discard any error raised by the source.
*/
def voidError(implicit ev: A <:< Unit): IO[Unit] = {
val _ = ev
asInstanceOf[IO[Unit]].handleError(_ => ())
Expand Down

0 comments on commit 40a7dde

Please sign in to comment.