Skip to content

Commit 1d0c935

Browse files
[stacktrace] Add support for directly inspecting ex-data
1 parent fbabdeb commit 1d0c935

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/cider/nrepl/middleware/stacktrace.clj

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
;; Analyze the last stacktrace
1919

20-
(def ^:dynamic *last-exception* nil)
21-
2220
(defn- analyze-last-stacktrace
2321
"Analyze the last exception."
2422
[{:keys [session] :as msg}]
2523
(let [last-exception (@session #'*e)]
26-
(swap! session assoc #'*last-exception* last-exception) ;; note that *e can change later, so this entry isn't redundant
24+
;; We need to remember the analyzed exception separately because we need to
25+
;; provide a way to inspect it while *e can change.
26+
(alter-meta! session assoc ::analyzed-exception last-exception)
2727
(send-analysis msg (stacktrace/analyze last-exception))))
2828

2929
(defn- handle-analyze-last-stacktrace-op
@@ -42,14 +42,22 @@
4242
(handle-analyze-last-stacktrace-op msg)
4343
(notify-client msg "The `stacktrace` op is deprecated, please use `analyze-last-stacktrace` instead." :warning))
4444

45+
(defn- get-last-exception-cause [{:keys [session index] :as msg}]
46+
(when index
47+
(let [last-exception (::analyzed-exception (meta session))
48+
causes (when last-exception
49+
(->> (iterate #(.getCause ^Throwable %) last-exception)
50+
(take-while some?)))]
51+
(nth causes index nil))))
52+
4553
(defn handle-inspect-last-exception-op [{:keys [session index transport] :as msg}]
46-
(let [last-exception (@session #'*last-exception*)
47-
causes (->> (iterate #(.getCause ^Throwable %) last-exception)
48-
(take-while some?))
49-
cause (when index
50-
(nth causes index nil))]
51-
(if cause
52-
(t/send transport (middleware.inspect/inspect-reply* msg cause))
54+
(let [inspect-ex-data? (= (:ex-data msg) "true")
55+
cause (get-last-exception-cause msg)
56+
object (if inspect-ex-data?
57+
(ex-data cause)
58+
cause)]
59+
(if object
60+
(t/send transport (middleware.inspect/inspect-reply* msg object))
5361
(respond-to msg :status :no-error))
5462
(respond-to msg :status :done)))
5563

0 commit comments

Comments
 (0)