Skip to content

Commit 7586c3c

Browse files
[print] Add custom print-method for throwables
1 parent f4aae69 commit 7586c3c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/orchard/print.clj

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,20 @@
163163
(defmethod print TaggedLiteral [x w]
164164
(print-method x w))
165165

166-
(defmethod print Throwable [x w]
167-
(print-method x w))
166+
(defmethod print Throwable [^Throwable x, ^TruncatingStringWriter w]
167+
(.write w "#Error[")
168+
(.write w (str (.getName (class x)) " "))
169+
(loop [cause x, msg nil]
170+
(if cause
171+
(recur (.getCause cause) (str msg (when msg ": ") (.getMessage cause)))
172+
(print msg w)))
173+
(when-let [data (not-empty (ex-data x))]
174+
(.write w " ")
175+
(print data w))
176+
(when-let [first-frame (first (.getStackTrace x))]
177+
(.write w " ")
178+
(print (str first-frame) w))
179+
(.write w "]"))
168180

169181
(defmethod print :default [^Object x, ^TruncatingStringWriter w]
170182
(.write w (.toString x)))

test/orchard/print_test.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@
8787
"#Delay[<pending>]" (delay 1)
8888
"#Delay[1]" (doto (delay 1) deref)
8989
"#Delay[<failed>]" (let [d (delay (/ 1 0))] (try @d (catch Exception _)) d)
90+
#"#Error\[clojure.lang.ExceptionInfo \"Boom\" \"orchard.print_test.+\"\]" (ex-info "Boom" {})
91+
#"#Error\[clojure.lang.ExceptionInfo \"Boom\" \{:a 1\} \"orchard.print_test.+\"\]" (ex-info "Boom" {:a 1})
92+
#"#Error\[java.lang.RuntimeException \"Runtime!\" \"orchard.print_test.+\"\]" (RuntimeException. "Runtime!")
93+
#"#Error\[java.lang.RuntimeException \"Outer: Inner\" \"orchard.print_test.+\"\]" (RuntimeException. "Outer"
94+
(RuntimeException. "Inner"))
9095
"#function[clojure.core/str]" str))
9196

9297
(deftest print-writer-limits

0 commit comments

Comments
 (0)