Skip to content

Commit 34c3b89

Browse files
author
dnolen
committed
CLJS-1376: Printing in a tagged literal data form
function and unknown object types print as #object ExceptionInfo printed as #error
1 parent 32e7559 commit 34c3b89

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8810,7 +8810,11 @@ reduces them without incurring seq initialization"
88108810
(-write writer obj))
88118811

88128812
^boolean (goog/isFunction obj)
8813-
(write-all writer "#<" (str obj) ">")
8813+
(let [name (.-name obj)
8814+
name (if (or (nil? name) (gstring/isEmpty name))
8815+
"Function"
8816+
name)]
8817+
(write-all writer "#object[" name " \"" (str obj) "\"]"))
88148818

88158819
(instance? js/Date obj)
88168820
(let [normalize (fn [n len]
@@ -8834,7 +8838,12 @@ reduces them without incurring seq initialization"
88348838
(implements? IPrintWithWriter obj)
88358839
(-pr-writer obj writer opts)
88368840

8837-
:else (write-all writer "#<" (str obj) ">")))))
8841+
:else
8842+
(let [name (.. obj -constructor -name)
8843+
name (if (or (nil? name) (gstring/isEmpty name))
8844+
"Object"
8845+
name)]
8846+
(write-all writer "#object[" name " " (str obj) "]"))))))
88388847

88398848
(defn- pr-writer
88408849
"Prefer this to pr-seq, because it makes the printing function
@@ -9838,7 +9847,7 @@ Maps become Objects. Arbitrary keys are encoded to by key->js."
98389847
;;; ExceptionInfo
98399848

98409849
(defn- pr-writer-ex-info [obj writer opts]
9841-
(-write writer "#ExceptionInfo{:message ")
9850+
(-write writer "#error {:message ")
98429851
(pr-writer (.-message obj) writer opts)
98439852
(when (.-data obj)
98449853
(-write writer ", :data ")

src/test/cljs/cljs/core_test.cljs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,10 +2022,10 @@
20222022
(catch ExceptionInfo e
20232023
(ex-data e)))))
20242024
(is (instance? js/Error (ex-info "asdf" {:foo 1})))
2025-
(is (= (pr-str (ex-info "abc" {:x 1})) "#ExceptionInfo{:message \"abc\", :data {:x 1}}"))
2026-
(is (= (pr-str (ex-info "abc" {:x 1} "def")) "#ExceptionInfo{:message \"abc\", :data {:x 1}, :cause \"def\"}"))
2027-
(is (= (.toString (ex-info "abc" {:x 1} "def")) "#ExceptionInfo{:message \"abc\", :data {:x 1}, :cause \"def\"}"))
2028-
(is (= (str (ex-info "abc" {:x 1} "def")) "#ExceptionInfo{:message \"abc\", :data {:x 1}, :cause \"def\"}"))
2025+
(is (= (pr-str (ex-info "abc" {:x 1})) "#error {:message \"abc\", :data {:x 1}}"))
2026+
(is (= (pr-str (ex-info "abc" {:x 1} "def")) "#error {:message \"abc\", :data {:x 1}, :cause \"def\"}"))
2027+
(is (= (.toString (ex-info "abc" {:x 1} "def")) "#error {:message \"abc\", :data {:x 1}, :cause \"def\"}"))
2028+
(is (= (str (ex-info "abc" {:x 1} "def")) "#error {:message \"abc\", :data {:x 1}, :cause \"def\"}"))
20292029
(is (not (instance? cljs.core.ExceptionInfo (js/Error.)))))
20302030

20312031
(deftest test-435

0 commit comments

Comments
 (0)