-
Notifications
You must be signed in to change notification settings - Fork 1.4k
add support for catching failures only, without defects #9488
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
base: series/2.x
Are you sure you want to change the base?
Changes from all commits
71a6336
573ffba
796e716
c3e74b9
2a0fc8f
44a6b85
a18a1e1
ff10117
5fcc9ee
769da16
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 |
---|---|---|
|
@@ -28,6 +28,13 @@ import scala.concurrent.ExecutionContext | |
import scala.reflect.ClassTag | ||
import scala.util.control.NoStackTrace | ||
import izumi.reflect.macrortti.LightTypeTag | ||
import zio.Cause.Empty | ||
import zio.Cause.Then | ||
import zio.Cause.Interrupt | ||
import zio.Cause.Die | ||
import zio.Cause.Stackless | ||
import zio.Cause.Fail | ||
import zio.Cause.Both | ||
|
||
/** | ||
* A `ZIO[R, E, A]` value is an immutable value (called an "effect") that | ||
|
@@ -297,12 +304,17 @@ sealed trait ZIO[-R, +E, +A] | |
self.foldTraceZIO[R1, E2, A1](h, ZIO.successFn) | ||
|
||
/** | ||
* Recovers from all errors with provided Cause. | ||
* Recovers from all errors and defects with provided Cause. | ||
* | ||
* {{{ | ||
* openFile("config.json").catchAllCause(_ => ZIO.succeed(defaultConfig)) | ||
* }}} | ||
* | ||
* '''WARNING''': There is no sensible way to recover from defects. This | ||
* method should be used only at the boundary between ZIO and an external | ||
* system, to transmit information on a defect for diagnostic or explanatory | ||
* purposes. Consider using `catchAll` or `catchFailureCause` instead. | ||
* | ||
* @see | ||
* [[absorb]], [[sandbox]], [[mapErrorCause]] - other functions that can | ||
* recover from defects | ||
|
@@ -329,6 +341,28 @@ sealed trait ZIO[-R, +E, +A] | |
): ZIO[R1, E1, A1] = | ||
catchSomeDefect { case t => h(t) } | ||
|
||
/** | ||
* Recovers from all failures with provided function. Equivalent to `catchAll` | ||
* but with the provided Cause. If you only care about the error, use | ||
* `catchAll` instead. | ||
* | ||
* This method doesn't recover from defects. If you need to recover from | ||
* defects, use `catchAllCause` instead. | ||
* | ||
* {{{ | ||
* effect.catchFailureCause(c => ZIO.logErrorCause("failure", c)) | ||
* }}} | ||
*/ | ||
final def catchFailureCause[R1 <: R, E2, A1 >: A]( | ||
h: Cause.Fail[E] => ZIO[R1, E2, A1] | ||
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. I still think this should be |
||
)(implicit ev: CanFail[E], trace: Trace): ZIO[R1, E2, A1] = | ||
self.foldTraceZIO[R1, E2, A1]( | ||
{ case (e, stackTrace) => | ||
h(Cause.Fail(e, stackTrace)) | ||
}, | ||
Comment on lines
+360
to
+362
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. If you were to change the signature, you could use |
||
ZIO.successFn | ||
) | ||
|
||
/** | ||
* Recovers from all NonFatal Throwables. | ||
* | ||
|
@@ -376,13 +410,18 @@ sealed trait ZIO[-R, +E, +A] | |
} | ||
|
||
/** | ||
* Recovers from some or all of the error cases with provided cause. | ||
* Recovers from some or all of the error or defect cases with provided cause. | ||
* | ||
* {{{ | ||
* openFile("data.json").catchSomeCause { | ||
* case c if (c.interrupted) => openFile("backup.json") | ||
* } | ||
* }}} | ||
* | ||
* '''WARNING''': There is no sensible way to recover from defects. This | ||
* method should be used only at the boundary between ZIO and an external | ||
* system, to transmit information on a defect for diagnostic or explanatory | ||
* purposes. Consider using `catchSomeFailureCause` instead. | ||
*/ | ||
final def catchSomeCause[R1 <: R, E1 >: E, A1 >: A]( | ||
pf: PartialFunction[Cause[E], ZIO[R1, E1, A1]] | ||
|
@@ -412,6 +451,33 @@ sealed trait ZIO[-R, +E, +A] | |
)(implicit trace: Trace): ZIO[R1, E1, A1] = | ||
unrefineWith(pf)(ZIO.failFn).catchAll(identity) | ||
|
||
/** | ||
* Recovers from some or all of the failure cases with provided cause. | ||
* | ||
* This method only recovers from failures. If you need to recover from | ||
* defects as well, use `catchSomeCause` or `catchSomeDefect` instead. | ||
* | ||
* {{{ | ||
* effect.catchSomeFailureCause { | ||
* case _: FileNotFoundException => createFile() | ||
* } | ||
* }}} | ||
*/ | ||
final def catchSomeFailureCause[R1 <: R, E1 >: E, A1 >: A]( | ||
pf: PartialFunction[Cause.Fail[E], ZIO[R1, E1, A1]] | ||
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. As above, I think we need to change |
||
)(implicit ev: CanFail[E], trace: Trace): ZIO[R1, E1, A1] = { | ||
def tryRescue(c: Cause[E]): ZIO[R1, E1, A1] = | ||
if (c.isFailureOnly) { | ||
c.find { case f: Cause.Fail[E] => f } match { | ||
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. I feel like |
||
case Some(f) => pf.applyOrElse(f, (_: Cause.Fail[E]) => Exit.failCause(c)) | ||
case None => Exit.failCause(c) | ||
} | ||
} else { | ||
Exit.failCause(c) | ||
} | ||
self.foldCauseZIO(tryRescue, ZIO.successFn) | ||
} | ||
|
||
/** | ||
* Returns an effect that succeeds with the cause of failure of this effect, | ||
* or `Cause.empty` if the effect did succeed. | ||
|
@@ -2101,6 +2167,10 @@ sealed trait ZIO[-R, +E, +A] | |
/** | ||
* Returns an effect that effectually "peeks" at the cause of the failure of | ||
* this effect. | ||
* | ||
* This method "peeks" at both the failure and defect of this effect. If you | ||
* only need to "peek" at the failure, use `tapFailureCause` instead. | ||
* | ||
* {{{ | ||
* readFile("data.json").tapErrorCause(logCause(_)) | ||
* }}} | ||
|
@@ -2121,6 +2191,26 @@ sealed trait ZIO[-R, +E, +A] | |
ZIO.successFn | ||
) | ||
|
||
/** | ||
* Returns an effect that effectfully "peeks" at the failure of this effect. | ||
* | ||
* This method only "peeks" at the failure of this effect. If you need to | ||
* "peek" at defects as well, use `tapErrorCause` or `tapDefect` instead. | ||
* | ||
* {{{ | ||
* readFile("data.json").tapError(logError(_)) | ||
* }}} | ||
*/ | ||
final def tapFailureCause[R1 <: R, E1 >: E]( | ||
f: Cause.Fail[E] => ZIO[R1, E1, Any] | ||
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. As above |
||
)(implicit ev: CanFail[E], trace: Trace): ZIO[R1, E1, A] = | ||
self.foldCauseZIO( | ||
c => | ||
c.find { case failure: Cause.Fail[E] => failure } | ||
.fold[ZIO[R1, E1, Nothing]](Exit.failCause(c))(f(_) *> Exit.failCause(c)), | ||
ZIO.successFn | ||
) | ||
|
||
/** | ||
* Returns an effect that effectfully "peeks" at the success of this effect. | ||
* If the partial function isn't defined at the input, the result is | ||
|
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.
Do you still think this one is needed?