Skip to content

Commit 3afdbe9

Browse files
committed
can we merge this super-duper useful util?
1 parent 82480c8 commit 3afdbe9

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

core/src/main/scala/com/avsystem/commons/SharedExtensions.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.avsystem.commons
22

33
import com.avsystem.commons.concurrent.RunNowEC
4-
import com.avsystem.commons.misc._
4+
import com.avsystem.commons.misc.*
55

66
import scala.annotation.{nowarn, tailrec}
77
import scala.collection.{AbstractIterator, BuildFrom, Factory, mutable}
88

99
trait SharedExtensions {
1010

11-
import com.avsystem.commons.SharedExtensionsUtils._
11+
import com.avsystem.commons.SharedExtensionsUtils.*
1212

1313
implicit def universalOps[A](a: A): UniversalOps[A] = new UniversalOps(a)
1414

@@ -471,8 +471,12 @@ object SharedExtensionsUtils extends SharedExtensions {
471471
def tapFailure(action: Throwable => Unit): Try[A] = tr match {
472472
case Success(_) => tr
473473
case Failure(throwable) =>
474-
action(throwable)
474+
try action(throwable)
475+
catch {
476+
case NonFatal(_) => // ignore any exceptions thrown by the action
477+
}
475478
tr
479+
476480
}
477481
}
478482

@@ -515,7 +519,7 @@ object SharedExtensionsUtils extends SharedExtensions {
515519

516520
class PartialFunctionOps[A, B](private val pf: PartialFunction[A, B]) extends AnyVal {
517521

518-
import PartialFunctionOps._
522+
import PartialFunctionOps.*
519523

520524
/**
521525
* The same thing as `orElse` but with arguments flipped.
@@ -651,7 +655,7 @@ object SharedExtensionsUtils extends SharedExtensions {
651655

652656
class MapOps[M[X, Y] <: BMap[X, Y], K, V](private val map: M[K, V]) extends AnyVal {
653657

654-
import MapOps._
658+
import MapOps.*
655659

656660
def getOpt(key: K): Opt[V] = map.get(key).toOpt
657661

core/src/test/scala/com/avsystem/commons/misc/SharedExtensionsTest.scala

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.avsystem.commons.misc
22

3+
import com.avsystem.commons.CommonAliases.*
4+
import com.avsystem.commons.SharedExtensions.*
35
import org.scalatest.funsuite.AnyFunSuite
46
import org.scalatest.matchers.should.Matchers
57

6-
import com.avsystem.commons.SharedExtensions._
7-
import com.avsystem.commons.CommonAliases._
8-
98
class SharedExtensionsTest extends AnyFunSuite with Matchers {
109
test("mkMap") {
1110
List.range(0, 3).mkMap(identity, _.toString) shouldEqual
@@ -81,7 +80,7 @@ class SharedExtensionsTest extends AnyFunSuite with Matchers {
8180
}
8281

8382
test("Future.transformWith") {
84-
import com.avsystem.commons.concurrent.RunNowEC.Implicits._
83+
import com.avsystem.commons.concurrent.RunNowEC.Implicits.*
8584
val ex = new Exception
8685
assert(Future.successful(42).transformWith(t => Future.successful(t.get - 1)).value.contains(Success(41)))
8786
assert(Future.successful(42).transformWith(_ => Future.failed(ex)).value.contains(Failure(ex)))
@@ -206,4 +205,34 @@ class SharedExtensionsTest extends AnyFunSuite with Matchers {
206205
| abc
207206
| abc""".stripMargin)
208207
}
208+
209+
test("Try.tapFailure - Success case") {
210+
var actionCalled = false
211+
val successTry = Success(42)
212+
val result = successTry.tapFailure(_ => actionCalled = true)
213+
214+
assert(!actionCalled, "Action should not be called for Success")
215+
assert(result === successTry, "Original Success should be returned")
216+
}
217+
218+
test("Try.tapFailure - Failure case") {
219+
var capturedThrowable: Throwable = null
220+
val exception = new RuntimeException("test exception")
221+
val failureTry = Failure(exception)
222+
223+
val result = failureTry.tapFailure(t => capturedThrowable = t)
224+
225+
assert(capturedThrowable === exception, "Action should be called with the exception")
226+
assert(result === failureTry, "Original Failure should be returned")
227+
}
228+
229+
test("Try.tapFailure - Exception in action") {
230+
val originalException = new RuntimeException("original exception")
231+
val actionException = new RuntimeException("action exception")
232+
val failureTry = Failure(originalException)
233+
234+
val result = failureTry.tapFailure(_ => throw actionException)
235+
236+
assert(result === failureTry, "Original Failure should be returned even if action throws")
237+
}
209238
}

0 commit comments

Comments
 (0)