-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bridge between ApplicativeError and ApplicativeThrow #4616
base: main
Are you sure you want to change the base?
Changes from 1 commit
8408933
366d776
e29d1d6
945dfd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
|
||
package cats | ||
|
||
import cats.ApplicativeError.CatchOnlyPartiallyApplied | ||
import cats.ApplicativeError.{CatchOnlyAsPartiallyApplied, CatchOnlyPartiallyApplied} | ||
import cats.data.{EitherT, Validated} | ||
import cats.data.Validated.{Invalid, Valid} | ||
|
||
|
@@ -287,6 +287,26 @@ trait ApplicativeError[F[_], E] extends Applicative[F] { | |
def catchOnly[T >: Null <: Throwable]: CatchOnlyPartiallyApplied[T, F, E] = | ||
new CatchOnlyPartiallyApplied[T, F, E](this) | ||
|
||
/** | ||
* Often E can be created from Throwable. Here we try to call pure or | ||
* catch, adapt into E, and raise. | ||
* | ||
* Exceptions that cannot be adapted to E will be propagated | ||
*/ | ||
def catchNonFatalAs[A](adaptIfPossible: Throwable => Option[E])(a: => A): F[A] = | ||
try pure(a) | ||
catch { | ||
case e if NonFatal(e) => adaptIfPossible(e).map(raiseError[A]).getOrElse(throw e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line concerns me quite a lot, particularly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless we require the adaptation function to be total, we're going to need to rethrow these, because there isn't a way to raise a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, exactly. This is why we must not use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @armanbilge , |
||
} | ||
|
||
/** | ||
* Evaluates the specified block, catching exceptions of the specified type and attempting to map them to `E`. | ||
* | ||
* Uncaught exceptions and those that cannot be adapted to E will be propagated. | ||
*/ | ||
def catchOnlyAs[T >: Null <: Throwable]: CatchOnlyAsPartiallyApplied[T, F, E] = | ||
morgen-peschke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new CatchOnlyAsPartiallyApplied[T, F, E](this) | ||
|
||
/** | ||
* If the error type is Throwable, we can convert from a scala.util.Try | ||
*/ | ||
|
@@ -383,6 +403,16 @@ object ApplicativeError { | |
} | ||
} | ||
|
||
final private[cats] class CatchOnlyAsPartiallyApplied[T >: Null <: Throwable, F[_], E]( | ||
private val F: ApplicativeError[F, E] | ||
) extends AnyVal { | ||
def apply[A](adaptIfPossible: T => Option[E])(f: => A)(implicit CT: ClassTag[T], NT: NotNull[T]): F[A] = | ||
try F.pure(f) | ||
catch { | ||
case CT(t) => adaptIfPossible(t).map(F.raiseError[A]).getOrElse(throw t) | ||
} | ||
} | ||
|
||
/** | ||
* lift from scala.Option[A] to a F[A] | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably prefer to deal with
PartialFunction
in such a case. Not only it allows to avoid an extra allocation, but also simpler to write a handler, e.g:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not mentioning that the handler would look more like a regular
catch
in that case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, using PFs for error handling is a common practice 👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect most times
adaptIfPossible
will be a method reference, so it would look like this:I'm open to being wrong about this, I just expect this sort of transformation won't generally written at the call sites because, if you need to do this, you probably need to do it more than once.