-
Notifications
You must be signed in to change notification settings - Fork 27
/
repl.clj
75 lines (63 loc) · 2.65 KB
/
repl.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(ns clj-commons.pretty.repl
"Utilities to assist with REPL-oriented development."
(:require [clj-commons.format.exceptions :as e :refer [print-exception]]
[clojure.main :as main]
[clojure.repl :as repl]
[clojure.stacktrace :as st])
(:import (clojure.lang RT)))
(defn- reset-var!
[v override]
(alter-var-root v (constantly override)))
(defn pretty-repl-caught
"A replacement for `clojure.main/repl-caught` that prints the exception to `*err*`, without a stack trace or properties."
[e]
(print-exception e {:frame-limit 0 :properties false}))
(defn uncaught-exception-handler
"Returns a reified UncaughtExceptionHandler that prints the formatted exception to `*err*`."
{:added "0.1.18"}
[]
(reify Thread$UncaughtExceptionHandler
(uncaughtException [_ _ t]
(binding [*out* *err*]
(printf "Uncaught exception in thread %s:%n%s%n"
(-> (Thread/currentThread) .getName)
(e/format-exception t))
(flush)))))
(defn pretty-pst
"Used as an override of `clojure.repl/pst` but uses pretty formatting."
([] (pretty-pst *e))
([e-or-depth]
(if (instance? Throwable e-or-depth)
(print-exception e-or-depth nil)
(pretty-pst *e e-or-depth)))
([e depth]
(binding [*out* *err*]
(print-exception e {:frame-limit depth}))))
(defn pretty-print-stack-trace
"Replacement for `clojure.stacktrace/print-stack-trace` and `print-cause-trace`. These functions are used by `clojure.test`."
([tr] (pretty-print-stack-trace tr nil))
([tr n]
(println)
(print-exception tr {:frame-limit n})))
(defn install-pretty-exceptions
"Installs an override that outputs pretty exceptions when caught by the main REPL loop. Also, overrides
`clojure.repl/pst`, `clojure.stacktrace/print-stack-trace`, `clojure.stacktrace/print-cause-trace`.
In addition, installs an [[uncaught-exception-handler]] so that uncaught exceptions in non-REPL threads
will be printed reasonably.
Caught exceptions do not print the stack trace; the pst replacement does."
[]
(reset-var! #'main/repl-caught pretty-repl-caught)
(reset-var! #'repl/pst pretty-pst)
(reset-var! #'st/print-stack-trace pretty-print-stack-trace)
(reset-var! #'st/print-cause-trace pretty-print-stack-trace)
;; This is necessary for Clojure 1.8 and above, due to direct linking
;; (from clojure.test to clojure.stacktrace).
(RT/loadResourceScript "clojure/test.clj")
(Thread/setDefaultUncaughtExceptionHandler (uncaught-exception-handler))
nil)
(defn -main
"Installs pretty exceptions, then delegates to clojure.main/main."
{:added "1.3.0"}
[& args]
(install-pretty-exceptions)
(apply main/main args))