Skip to content

Commit 492bb9c

Browse files
committed
Don't blow up on unresolvable pprint functions
First try to require their namespace and resolve them, afterwards fallback to a safe default.
1 parent c1346cf commit 492bb9c

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/cider/nrepl/middleware/pprint.clj

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
1818
;; TODO: Remove this middleware (or make it a no-op).
1919

2020
(defn- resolve-pprint-fn
21-
[sym]
22-
(if-let [pp-fn (-> sym u/as-sym find-var)]
23-
pp-fn
24-
(throw (IllegalArgumentException. (format "%s is not resolvable to a var" sym)))))
21+
"Resolve a namespaced symbol to a printer var. Returns the var or nil if
22+
the argument is nil or not resolvable."
23+
[var-sym]
24+
(when-let [var-sym (and var-sym (symbol var-sym))]
25+
(try
26+
(require (symbol (namespace var-sym)))
27+
(resolve var-sym)
28+
(catch Exception ex
29+
(nrepl.misc/log ex "Couldn't resolve printer function" var-sym)
30+
(require 'cider.nrepl.pprint)
31+
(resolve 'cider.nrepl.pprint/pprint)))))
2532

2633
(defn handle-pprint-fn
2734
[handler msg]

test/clj/cider/nrepl/middleware/format_test.clj

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,9 @@
143143
(is (= "{:a 1, :b 2, :c 3}" formatted-edn))
144144
(is (= #{"done"} status))))
145145

146-
#_(testing "format-edn returns an error if the :pprint-fn is unresolvable"
147-
(let [{:keys [err ex status] :as response} (session/message {:op "format-edn"
148-
:edn "{:b 2 :c 3 :a 1}"
149-
:pprint-fn "fake.nrepl.pprint/puget-pprint"})]
150-
(is (.startsWith err "java.lang.IllegalArgumentException: No such namespace: fa"))
151-
(is (= "class java.lang.IllegalArgumentException" ex))
152-
(is (= #{"done" "format-edn-error"} status))
153-
(is (:pp-stacktrace response)))))
146+
(testing "format-edn returns fallbacks to a default printer if :pprint-fn is unresolvable"
147+
(let [{:keys [formatted-edn status] :as response} (session/message {:op "format-edn"
148+
:edn "{:b 2 :c 3 :a 1}"
149+
:pprint-fn "fake.nrepl.pprint/puget-pprint"})]
150+
(is (= "{:b 2, :c 3, :a 1}" formatted-edn))
151+
(is (= #{"done"} status)))))

0 commit comments

Comments
 (0)