cats-mtl icon indicating copy to clipboard operation
cats-mtl copied to clipboard

handle error with partial function

Open prophe05 opened this issue 2 years ago • 0 comments

It would be nice to add to Handle methods that takes PartialFunction something like ApplicativeError has (recover and recoverWith). For all unmatched cases raise an exception with ApplicativeError, something like:

def handlePartially[A](fa: F[A])(pf: PartialFunction[E, A])(implicit ap: ApplicativeError[F, Throwable]): F[A] =
 handleWith[A](fa)(e => pf.andThen(applicative.pure[A](_)).applyOrElse[E, F[A]](e, me => ap.raiseError(new MatchError(me))))

And one step further, it would be super awesome if it would be possible to do like: some_code.handlePartiallyWith[NarrowError](_.asLeft[Unit].pure[F])

prophe05 avatar Sep 02 '22 15:09 prophe05