Skip to content

Commit 7c6215d

Browse files
[stacktrace] Replace customizable pretty-printer with orchard.print
1 parent 3ad3c10 commit 7c6215d

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

src/orchard/stacktrace.clj

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@
33
objects and attach extra data to them."
44
{:added "0.31"
55
:author "Jeff Valk, Oleksandr Yakushev"}
6+
(:refer-clojure :exclude [print-str])
67
(:require
78
[clojure.java.io :as io]
89
[clojure.main]
9-
[clojure.pprint :as pp]
1010
[clojure.repl :as repl]
1111
[clojure.spec.alpha :as s]
1212
[clojure.string :as str]
1313
[orchard.info :as info]
1414
[orchard.java.resource :as resource]
15-
[orchard.misc :as misc :refer [assoc-some]])
15+
[orchard.misc :as misc :refer [assoc-some]]
16+
[orchard.print :as print])
1617
(:import
17-
(java.io StringWriter)
1818
(java.net URL)
1919
(java.nio.file Path)))
2020

2121
(def ^:private ^Path cwd-path (.toAbsolutePath (.toPath (io/file ""))))
2222

23-
(defn- pprint-write
24-
"We don't use `clojure.pprint/pprint` directly because it appends a newline at
25-
the end which we don't want."
26-
[value writer]
27-
(pp/write value :stream writer))
23+
(defn- print-str [value]
24+
;; Limit printed collections to 5 items.
25+
(binding [*print-length* 5]
26+
(print/print-str value)))
2827

2928
;;; ## Stacktraces
3029

@@ -221,18 +220,18 @@
221220

222221
(defn- prepare-spec-data
223222
"Prepare spec problems for display in user stacktraces. Take in a map `ed` as
224-
returned by `clojure.spec.alpha/explain-data` and return a map of pretty
225-
printed problems. The content of the returned map is modeled after
223+
returned by `clojure.spec.alpha/explain-data` and return a map of printed
224+
problems. The content of the returned map is modeled after
226225
`clojure.spec.alpha/explain-printer`."
227-
[ed pprint-str]
226+
[ed]
228227
(let [problems (sort-by #(count (:path %)) (::s/problems ed))]
229228
{:spec (pr-str (::s/spec ed))
230-
:value (pprint-str (::s/value ed))
229+
:value (print-str (::s/value ed))
231230
:problems
232231
(mapv
233232
(fn [{:keys [in val pred reason via path] :as prob}]
234-
(->> {:in (some-> in not-empty pr-str)
235-
:val (pprint-str val)
233+
(->> {:in (some-> in not-empty print-str)
234+
:val (print-str val)
236235
:predicate (pr-str (s/abbrev pred))
237236
:reason reason
238237
:spec (some-> via not-empty last pr-str)
@@ -243,7 +242,7 @@
243242
::s/failure} (key %)))
244243
prob)]
245244
(when (seq extras)
246-
(pprint-str extras)))}
245+
(print-str extras)))}
247246
(filter clojure.core/val)
248247
(into {})))
249248
problems)}))
@@ -258,11 +257,8 @@
258257

259258
(defn- analyze-cause
260259
"Analyze the `cause-data` of an exception, in `Throwable->map` format."
261-
[cause-data print-fn]
262-
(let [pprint-str #(let [writer (StringWriter.)]
263-
(print-fn % writer)
264-
(str writer))
265-
phase (-> cause-data :data :clojure.error/phase)
260+
[cause-data]
261+
(let [phase (-> cause-data :data :clojure.error/phase)
266262
m (-> {:class (name (:type cause-data))
267263
:phase phase
268264
:message (:message cause-data)
@@ -274,9 +270,9 @@
274270
(if (::s/failure data)
275271
(assoc m
276272
:message "Spec assertion failed."
277-
:spec (prepare-spec-data data pprint-str))
273+
:spec (prepare-spec-data data))
278274
(assoc m
279-
:data (pprint-str data)
275+
:data (print-str data)
280276
:location (select-keys data [:clojure.error/line
281277
:clojure.error/column
282278
:clojure.error/phase
@@ -297,7 +293,7 @@
297293

298294
(defn- analyze-causes
299295
"Analyze the cause chain of the `exception-data` in `Throwable->map` format."
300-
[exception-data print-fn]
296+
[exception-data]
301297
(let [triage-message (maybe-triage-message exception-data)
302298
causes (update (vec (:via exception-data)) 0
303299
#(cond-> %
@@ -306,19 +302,16 @@
306302
(nil? (:trace %)) (assoc :trace (:trace exception-data))
307303
;; If non-nil, assoc triage-message to first cause.
308304
triage-message (assoc :triage triage-message)))]
309-
(mapv #(extract-location (analyze-cause % print-fn)) causes)))
305+
(mapv #(extract-location (analyze-cause %)) causes)))
310306

311307
(defn analyze
312-
"Return the analyzed cause chain for `exception` beginning with the
313-
thrown exception. `exception` can be an instance of `Throwable` or a
314-
map in the same format as `Throwable->map`. For `ex-info`
315-
exceptions, the response contains a `:data` slot with the pretty
316-
printed data. For clojure.spec asserts, the `:spec` slot contains a
317-
map of pretty printed components describing spec failures."
318-
([exception]
319-
(analyze exception pprint-write))
320-
([exception print-fn]
321-
(cond (instance? Throwable exception)
322-
(analyze-causes (Throwable->map-with-traces exception) print-fn)
323-
(and (map? exception) (:trace exception))
324-
(analyze-causes exception print-fn))))
308+
"Return the analyzed cause chain for `exception` beginning with the thrown
309+
exception. `exception` can be an instance of `Throwable` or a map in the same
310+
format as `Throwable->map`. For `ex-info` exceptions, the response contains a
311+
`:data` slot with the printed data. For clojure.spec asserts, the `:spec` slot
312+
contains a map of printed components describing spec failures."
313+
[exception]
314+
(cond (instance? Throwable exception)
315+
(analyze-causes (Throwable->map-with-traces exception))
316+
(and (map? exception) (:trace exception))
317+
(analyze-causes exception)))

0 commit comments

Comments
 (0)