Skip to content
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

Don't try to catch fatal exceptions... #53

Merged
merged 1 commit into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ Multiple `Managed` instances are composed or stacked via `flatMap`, generally wi

Once the `Managed` stack is composed, the underlying resources are built and used with `use` or `useUntilShutdown`. Setup occurs in the order of the for comprehension or `flatMap`s, and teardown happens in the reverse order.

Exception behavior is as follows:
Non-fatal exceptions are handled as follows:
- Exceptions during setup are thrown after already-built resources are torn down
- Exceptions during usage are thrown after resources are torn down
- Exceptions during teardown are thrown, but only after teardown is called on every resource.
- Exceptions during teardown are thrown, but only after teardown is called on every resource.
- If an exception is thrown during usage, and an additional exception occurs during teardown, the usage exception is thrown with the teardown exception added as a suppressed exception.
- If an exception is thrown during setup, and an additional exception occurs during teardown, the setup exception is thrown with the teardown exception added as a suppressed exception.

Expand Down
13 changes: 7 additions & 6 deletions managerial/src/main/scala/ca/dvgi/managerial/Managed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ca.dvgi.managerial
import scala.util.Try
import scala.util.Failure
import scala.util.Success
import scala.util.control.NonFatal

/** An instance of Managed wraps a resource and manages its lifecycle.
*
Expand All @@ -22,14 +23,14 @@ trait Managed[+T] { selfT =>
var toThrow: Throwable = null
try f(r.get)
catch {
case t: Throwable =>
case NonFatal(t) =>
toThrow = t
null.asInstanceOf[R] // compiler doesn't know that finally will throw
} finally {
try {
r.teardown()
} catch {
case t: Throwable =>
case NonFatal(t) =>
if (toThrow == null) toThrow = t
else toThrow.addSuppressed(t)
}
Expand All @@ -47,8 +48,8 @@ trait Managed[+T] { selfT =>
* or teardown. By default, exceptions on setup or teardown are thrown.
*/
def useUntilShutdown(
onSetupException: PartialFunction[Throwable, Unit] = { case t: Throwable => throw t },
onTeardownException: PartialFunction[Throwable, Unit] = { case t: Throwable => throw t }
onSetupException: PartialFunction[Throwable, Unit] = { case NonFatal(t) => throw t },
onTeardownException: PartialFunction[Throwable, Unit] = { case NonFatal(t) => throw t }
): Unit = {
var r: Resource[T] = null

Expand All @@ -75,11 +76,11 @@ trait Managed[+T] { selfT =>
try {
f(t.get).build()
} catch {
case setupThrowable: Throwable =>
case NonFatal(setupThrowable) =>
try {
t.teardown()
} catch {
case teardownThrowable: Throwable =>
case NonFatal(teardownThrowable) =>
setupThrowable.addSuppressed(teardownThrowable)
}

Expand Down