cats-mtl
cats-mtl copied to clipboard
handle error with partial function
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])