Skip to content

Commit ea22e7a

Browse files
committed
Make catch and finally ->> friendly
Base on @mbossenbroek's feedback. Seems like an improvement.
1 parent c51054e commit ea22e7a

File tree

3 files changed

+87
-79
lines changed

3 files changed

+87
-79
lines changed

language-adaptors/rxjava-clojure/src/main/clojure/rx/lang/clojure/core.clj

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -798,88 +798,103 @@
798798

799799
(defn catch*
800800
"Returns an observable that, when Observable o triggers an error, e, continues with
801-
Observable returned by (apply f e args) if (p e) is true. If (p e) returns a Throwable
801+
Observable returned by (f e) if (p e) is true. If (p e) returns a Throwable
802802
that value is passed as e.
803803
804804
If p is a class object, a normal instance? check is performed rather than calling it
805805
as a function. If the value returned by (p e) is not true, the error is propagated.
806806
807807
Examples:
808808
809-
(-> my-observable
809+
(->> my-observable
810810
811-
; On IllegalArgumentException, just emit 1
812-
(catch* IllegalArgumentException (fn [e] (rx/return 1)))
811+
; On IllegalArgumentException, just emit 1
812+
(catch* IllegalArgumentException
813+
(fn [e] (rx/return 1)))
813814
814-
; If exception message contains \"WAT\", emit [\\W \\A \\T]
815-
(catch* #(-> % .getMessage (.contains \"WAT\")) (rx/seq->o [\\W \\A \\T])))
815+
; If exception message contains \"WAT\", emit [\\W \\A \\T]
816+
(catch* (fn [e] (-> e .getMessage (.contains \"WAT\")))
817+
(fn [e] (rx/seq->o [\\W \\A \\T]))))
816818
817819
See:
818-
820+
rx.Observable/onErrorResumeNext
819821
http://netflix.github.io/RxJava/javadoc/rx/Observable.html#onErrorResumeNext(rx.util.functions.Func1)
820822
"
821-
[^Observable o p f & args]
823+
[p f ^Observable o]
822824
(let [p (if (class? p)
823825
(fn [e] (.isInstance ^Class p e))
824826
p)]
825827
(.onErrorResumeNext o
826828
^Func1 (iop/fn [e]
827829
(if-let [maybe-e (p e)]
828-
(apply f (if (instance? Throwable maybe-e) maybe-e e) args)
830+
(f (if (instance? Throwable maybe-e)
831+
maybe-e
832+
e))
829833
(rx.lang.clojure.core/throw e))))))
830834

831835
(defmacro catch
832836
"Macro version of catch*.
833837
834838
The body of the catch is wrapped in an implicit (do). It must evaluate to an Observable.
835839
836-
Note that the source observable is the first argument so this won't mix well with ->>
837-
threading.
840+
Note that the source observable is the last argument so this works with ->> but may look
841+
slightly odd when used standalone.
838842
839843
Example:
840844
841-
(-> my-observable
842-
; just emit 0 on IllegalArgumentException
843-
(catch IllegalArgumentException e
844-
(rx/return 0))
845+
(->> my-observable
846+
; just emit 0 on IllegalArgumentException
847+
(catch IllegalArgumentException e
848+
(rx/return 0))
845849
846-
(catch DependencyException e
847-
(if (.isMinor e)
848-
(rx/return 0)
849-
(rx/throw (WebException. 503)))))
850+
(catch DependencyException e
851+
(if (.isMinor e)
852+
(rx/return 0)
853+
(rx/throw (WebException. 503)))))
850854
851855
See:
852856
catch*
853857
"
854-
[o p binding & body]
855-
`(catch* ~o ~p (fn [~binding] ~@body)))
858+
{:arglists '([p binding & body observable])}
859+
[p binding & body]
860+
(let [o (last body)
861+
body (butlast body)]
862+
`(catch* ~p
863+
(fn [~binding] ~@body)
864+
~o)))
856865

857866
(defn finally*
858-
"Returns an Observable that, as a side-effect, executes (apply f args) when the given
867+
"Returns an Observable that, as a side-effect, executes (f) when the given
859868
Observable completes regardless of success or failure.
860869
861870
Example:
862871
863-
(-> my-observable
864-
(finally* (fn [] (println \"Done\"))))
872+
(->> my-observable
873+
(finally* (fn [] (println \"Done\"))))
865874
866875
"
867-
[^Observable o f & args]
868-
(.finallyDo o ^Action0 (iop/action [] (apply f args))))
876+
[f ^Observable o]
877+
(.finallyDo o ^Action0 (iop/action* f)))
869878

870879
(defmacro finally
871880
"Macro version of finally*.
872881
882+
Note that the source observable is the last argument so this works with ->> but may look
883+
slightly odd when used standalone.
884+
873885
Example:
874886
875-
(-> my-observable
876-
(finally (println \"Done\")))
887+
(->> my-observable
888+
(finally (println \"Done\")))
877889
878890
See:
879891
finally*
880892
"
881-
[o & body]
882-
`(finally* ~o (fn [] ~@body)))
893+
{:arglists '([& body observable])}
894+
[& body]
895+
(let [o (last body)
896+
body (butlast body)]
897+
`(finally* (fn [] ~@body) ~o)))
883898

884899
;################################################################################;
885900

language-adaptors/rxjava-clojure/src/test/clojure/rx/lang/clojure/core_test.clj

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -531,33 +531,30 @@
531531
(deftest test-catch*
532532
(testing "Is just a passthrough if there's no error"
533533
(is (= [1 2 3]
534-
(b/into []
535-
(->
536-
(rx/seq->o [1 2 3])
537-
(rx/catch* Exception (fn [e] (throw "OH NO"))))))))
534+
(->> (rx/seq->o [1 2 3])
535+
(rx/catch* Exception (fn [e] (throw "OH NO")))
536+
(b/into [])))))
538537

539538
(testing "Can catch a particular exception type and continue with an observable"
540539
(is (= [1 2 4 5 6 "foo"]
541-
(b/into []
542-
(->
543-
(rx/generator [o]
544-
(rx/on-next o 1)
545-
(rx/on-next o 2)
546-
(rx/on-error o (IllegalStateException. "foo")))
547-
(rx/catch* IllegalStateException
548-
(fn [e]
549-
(rx/seq->o [4 5 6 (.getMessage e)]))))))))
540+
(->> (rx/generator [o]
541+
(rx/on-next o 1)
542+
(rx/on-next o 2)
543+
(rx/on-error o (IllegalStateException. "foo")))
544+
(rx/catch* IllegalStateException
545+
(fn [e]
546+
(rx/seq->o [4 5 6 (.getMessage e)])))
547+
(b/into [])))))
550548

551549
(testing "if exception isn't matched, it's passed to on-error"
552550
(let [expected (IllegalArgumentException. "HI")
553551
called (atom nil)]
554-
(rx/subscribe (->
555-
(rx/generator [o]
556-
(rx/on-next o 1)
557-
(rx/on-next o 2)
558-
(rx/on-error o expected))
559-
(rx/catch* IllegalStateException (fn [e]
560-
(rx/return "WAT?"))))
552+
(rx/subscribe (->> (rx/generator [o]
553+
(rx/on-next o 1)
554+
(rx/on-next o 2)
555+
(rx/on-error o expected))
556+
(rx/catch* IllegalStateException (fn [e]
557+
(rx/return "WAT?"))))
561558
(fn [_])
562559
(fn [e] (reset! called expected))
563560
(fn [_]))
@@ -567,23 +564,19 @@
567564
(let [cause (IllegalArgumentException. "HI")
568565
wrapper (java.util.concurrent.ExecutionException. cause)]
569566
(is (= [cause]
570-
(b/into []
571-
(->
572-
(rx/generator [o]
573-
(rx/on-error o wrapper))
574-
(rx/catch #(.getCause %) e
575-
(rx/return e)))))))))
567+
(->> (rx/throw wrapper)
568+
(rx/catch #(.getCause %) e
569+
(rx/return e))
570+
(b/into [])))))))
576571

577572

578573
(deftest test-finally
579574
(testing "Supports a finally clause"
580575
(testing "called on completed"
581576
(let [completed (atom nil)
582577
called (atom nil)]
583-
(rx/subscribe (->
584-
(rx/seq->o [1 2 3])
585-
(rx/finally* (fn [extra] (reset! called (str "got " extra)))
586-
"it"))
578+
(rx/subscribe (->> (rx/seq->o [1 2 3])
579+
(rx/finally* (fn [] (reset! called (str "got it")))))
587580
(fn [_])
588581
(fn [_] (throw (IllegalStateException. "WAT")))
589582
(fn [] (reset! completed "DONE")))
@@ -594,13 +587,12 @@
594587
(let [expected (IllegalStateException. "expected")
595588
completed (atom nil)
596589
called (atom nil)]
597-
(rx/subscribe (->
598-
(rx/generator [o]
599-
(rx/on-next o 1)
600-
(rx/on-next o 2)
601-
(rx/on-error o expected))
602-
(rx/finally
603-
(reset! called "got it")))
590+
(rx/subscribe (->> (rx/generator [o]
591+
(rx/on-next o 1)
592+
(rx/on-next o 2)
593+
(rx/on-error o expected))
594+
(rx/finally
595+
(reset! called "got it")))
604596
(fn [_])
605597
(fn [e] (reset! completed e))
606598
(fn [] (throw (IllegalStateException. "WAT"))))

language-adaptors/rxjava-clojure/src/test/clojure/rx/lang/clojure/future_test.clj

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
(deftest test-future-exception
1919
(is (= "Caught: boo"
20-
(-> (f/future f/default-runner (throw (java.io.FileNotFoundException. "boo")))
21-
(rx/catch java.io.FileNotFoundException e
22-
(rx/return (str "Caught: " (.getMessage e))))
23-
(b/single)))))
20+
(->> (f/future f/default-runner (throw (java.io.FileNotFoundException. "boo")))
21+
(rx/catch java.io.FileNotFoundException e
22+
(rx/return (str "Caught: " (.getMessage e))))
23+
(b/single)))))
2424

2525
(deftest test-future-cancel
2626
(let [exited? (atom nil)
@@ -52,11 +52,12 @@
5252
(deftest test-future-generator-exception
5353
(let [e (java.io.FileNotFoundException. "snake")]
5454
(is (= [1 2 e]
55-
(b/into [] (-> (f/future-generator
56-
f/default-runner
57-
[o]
58-
(rx/on-next o 1)
59-
(rx/on-next o 2)
60-
(throw e))
61-
(rx/catch java.io.FileNotFoundException e
62-
(rx/return e))))))))
55+
(->> (f/future-generator
56+
f/default-runner
57+
[o]
58+
(rx/on-next o 1)
59+
(rx/on-next o 2)
60+
(throw e))
61+
(rx/catch java.io.FileNotFoundException e
62+
(rx/return e))
63+
(b/into []))))))

0 commit comments

Comments
 (0)