Skip to content

Commit 7c82720

Browse files
committed
Simplify the code for Using mechanics. Thanks @coderabbitai!
1 parent f43d363 commit 7c82720

2 files changed

Lines changed: 14 additions & 23 deletions

File tree

pramen/core/src/main/scala/za/co/absa/pramen/core/utils/UsingUtils.scala

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,40 @@
1616

1717
package za.co.absa.pramen.core.utils
1818

19-
import scala.util.control.NonFatal
20-
2119
object UsingUtils {
2220
/**
2321
* Executes the given action with a resource that implements the AutoCloseable interface, ensuring
2422
* proper closure of the resource. Any exception that occurs during the action or resource closure
25-
* is handled appropriately, with suppressed exceptions added where relevant. Null resources are not supported.
23+
* is handled appropriately, with suppressed exceptions added where relevant.
2624
*
2725
* @param resource a lazily evaluated resource that implements AutoCloseable
2826
* @param action a function to be executed using the provided resource
2927
* @tparam T the type of the resource, which must extend AutoCloseable
3028
* @throws Throwable if either the action or resource closure fails. If both fail, the action's exception
3129
* is thrown with the closure's exception added as suppressed
3230
*/
33-
def using[T <: AutoCloseable,U](resource: => T)(action: T => U): U = {
34-
var actionExceptionOpt: Option[Throwable] = None
31+
def using[T <: AutoCloseable, U](resource: => T)(action: T => U): U = {
32+
var actionException: Throwable = null
3533
val openedResource = resource
3634

3735
try {
38-
return action(openedResource)
36+
action(openedResource)
3937
} catch {
40-
case NonFatal(ex) =>
41-
actionExceptionOpt = Option(ex)
38+
case t: Throwable =>
39+
actionException = t
40+
throw t
4241
} finally
4342
if (openedResource != null) {
4443
try
4544
openedResource.close()
4645
catch {
47-
case NonFatal(closeException) =>
48-
actionExceptionOpt match {
49-
case Some(actionException) =>
50-
actionException.addSuppressed(closeException)
51-
throw actionException
52-
case None =>
53-
throw closeException
46+
case closeException: Throwable =>
47+
if (actionException != null) {
48+
actionException.addSuppressed(closeException)
49+
} else {
50+
throw closeException
5451
}
5552
}
5653
}
57-
58-
// It is not possible to return a valid value of type U at this point so the rest of code should return Nothing
59-
actionExceptionOpt match {
60-
case Some(ex) => throw ex
61-
case None => throw new IllegalArgumentException("Unreachable code")
62-
}
6354
}
6455
}

pramen/core/src/test/scala/za/co/absa/pramen/core/tests/utils/UsingUtilsSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class UsingUtilsSuite extends AnyWordSpec {
5454
assert(resource.closeCallCount == 1)
5555
}
5656

57-
"handle a null resource resource" in {
57+
"handle a null resource" in {
5858
var resourceWasNull = false
5959

6060
UsingUtils.using(null: AutoCloseableSpy) { res =>
@@ -64,7 +64,7 @@ class UsingUtilsSuite extends AnyWordSpec {
6464
assert(resourceWasNull)
6565
}
6666

67-
"handle a null resource resource and action throw" in {
67+
"handle a null resource and action throw" in {
6868
var exceptionThrown = false
6969
var resourceWasNull = false
7070

0 commit comments

Comments
 (0)