Skip to content

Commit 366a72a

Browse files
committed
Indent multi-line messages
Reduce the indentation of properties (within exceptions) Fixes #11
1 parent 58c4abc commit 366a72a

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/io/aviso/exception.clj

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@
126126
(indent writer (- width (.length value)))
127127
(w/write writer prefix value suffix)))
128128

129+
(defn- write-indented
130+
[writer indent-amount value]
131+
(loop [lines (str/split-lines value)
132+
is-first true]
133+
(when-not (empty? lines)
134+
(if-not is-first
135+
(indent writer indent-amount))
136+
(w/writeln writer (first lines))
137+
(recur (rest lines) false))))
138+
129139
(defn- update-keys [m f]
130140
"Builds a map where f has been applied to each key in m."
131141
(into {} (map (fn [[k v]] [(f k) v]) m)))
@@ -138,8 +148,8 @@
138148
;; In a degenerate case, a protocol method could be called "invoke" or "doInvoke"; we're ignoring
139149
;; that possibility here and assuming it's the IFn.invoke() or doInvoke().
140150
all-ids (if (contains? #{"invoke" "doInvoke"} method-name)
141-
function-ids
142-
(-> function-ids vec (conj method-name)))]
151+
function-ids
152+
(-> function-ids vec (conj method-name)))]
143153
;; The assumption is that no real namespace or function name will contain underscores (the underscores
144154
;; are name-mangled dashes).
145155
(->>
@@ -221,19 +231,9 @@
221231
(justify class-width class)
222232
(w/writeln "." method)))))
223233

224-
225234
(defn- write-property-value [writer value-indent value]
226-
(loop [lines (-> value
227-
(pp/write :stream nil :length (or *print-length* 10))
228-
(str/split-lines))
229-
is-first true]
230-
(if (empty? lines)
231-
nil
232-
(do
233-
(if-not is-first
234-
(indent writer value-indent))
235-
(w/writeln writer (first lines))
236-
(recur (rest lines) false)))))
235+
(write-indented writer value-indent
236+
(pp/write value :stream nil :length (or *print-length* 10))))
237237

238238
(defn write-exception
239239
"Writes a formatted version of the exception to the writer.
@@ -258,19 +258,20 @@
258258
message (.getMessage exception)]
259259
(justify writer exception-column-width exception-font (:name e) reset-font)
260260
;; TODO: Handle no message for the exception specially
261-
(w/write writer ":"
262-
(if message
263-
(str " " message-font message reset-font)
264-
"")
265-
w/endline)
261+
(w/write writer ":")
262+
(if-not message
263+
(w/writeln writer)
264+
(do
265+
(w/write writer " ")
266+
(write-indented writer
267+
(+ 2 exception-column-width)
268+
(str message-font message reset-font))))
266269

267270
(let [properties (update-keys (:properties e) name)
268271
prop-keys (keys properties)
269272
;; Allow for the width of the exception class name, and some extra
270273
;; indentation.
271-
prop-name-width (+ exception-column-width
272-
4
273-
(max-length prop-keys))]
274+
prop-name-width (+ 4 (max-length prop-keys))]
274275
(doseq [k (sort prop-keys)]
275276
(justify writer prop-name-width property-font k reset-font)
276277
(w/write writer ": ")

test/user.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
(defn- jdbc-update
77
[]
8-
(throw (SQLException. "Database failure" "ABC" 123)))
8+
(throw (SQLException. "Database failure\nSELECT FOO, BAR, BAZ\nFROM GNIP\nfailed with ABC123" "ABC" 123)))
99

1010
(defprotocol Worker
1111
(do-work [this]))

0 commit comments

Comments
 (0)