File tree 2 files changed +31
-0
lines changed
main/scala/wvlet/airframe/rx
test/scala/wvlet/airframe/rx
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -312,6 +312,12 @@ trait Rx[+A] extends RxOps[A] {
312
312
TransformRxOp (this , f)
313
313
}
314
314
315
+ /**
316
+ * Transform a Success(v) or Failure(Throwable) input with a given function.
317
+ * @param f
318
+ * @tparam B
319
+ * @return
320
+ */
315
321
def transform [B ](f : Try [A ] => B ): Rx [B ] = {
316
322
TransformOp (this , f)
317
323
}
@@ -324,6 +330,18 @@ trait Rx[+A] extends RxOps[A] {
324
330
TransformTryOp (this , f)
325
331
}
326
332
333
+ /**
334
+ * Transform a specific type of an exception into another exception. This is useful for handling exceptions.
335
+ * @param f
336
+ * @return
337
+ */
338
+ def transformFailure (f : PartialFunction [Throwable , Throwable ]): Rx [A ] = {
339
+ transformTry[A ] {
340
+ case Failure (ex) if f.isDefinedAt(ex) => Failure (f.apply(ex))
341
+ case other => other
342
+ }
343
+ }
344
+
327
345
def concat [A1 >: A ](other : Rx [A1 ]): Rx [A1 ] = Rx .concat(this , other)
328
346
def lastOption : RxOption [A ] = LastOp (this ).toOption
329
347
Original file line number Diff line number Diff line change @@ -95,4 +95,17 @@ class RxTransformTest extends AirSpec {
95
95
fail(s " Unexpected: ${other}" )
96
96
}
97
97
}
98
+
99
+ test(" Rx.transformFailure" ) {
100
+ Rx .fromTry(Failure (new UnsupportedOperationException (" N/A" )))
101
+ .transformFailure { case ex : UnsupportedOperationException =>
102
+ new IllegalStateException (" unimplemented" )
103
+ }
104
+ .recover {
105
+ case e : IllegalStateException =>
106
+ " ok"
107
+ case other =>
108
+ fail(s " Unexpected: ${other}" )
109
+ }
110
+ }
98
111
}
You can’t perform that action at this time.
0 commit comments