Skip to content

[stacktrace] Add ex-str-formatted message to analyzed causes #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[stacktrace] Add ex-str-formatted message to analyzed causes
  • Loading branch information
alexander-yakushev committed Mar 19, 2025
commit 68bc6152e226b4bbda9fe97edce86f8ec655903f
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

* [#325](https://github.com/clojure-emacs/orchard/pull/325): Add `ex-str`-formatted message to analyzed causes.

## 0.31.0 (2025-03-14)

* [#317](https://github.com/clojure-emacs/orchard/pull/317): **BREAKING:** Remove deprecated functions:
Expand Down
5 changes: 5 additions & 0 deletions src/orchard/misc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
(last xs)))]
(apply f (filter identity xs))))

(defn assoc-some
"Assoc key-value to the map `m` if `v` is non-nil."
[m k v]
(if (nil? v) m (assoc m k v)))

(defn parse-java-version
"Parse a Java version string according to JEP 223 and return the appropriate
version."
Expand Down
58 changes: 38 additions & 20 deletions src/orchard/stacktrace.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
:author "Jeff Valk, Oleksandr Yakushev"}
(:require
[clojure.java.io :as io]
[clojure.main]
[clojure.pprint :as pp]
[clojure.repl :as repl]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[orchard.info :as info]
[orchard.java.resource :as resource])
[orchard.java.resource :as resource]
[orchard.misc :refer [assoc-some]])
(:import
(java.io StringWriter)
(java.net URL)
Expand Down Expand Up @@ -166,14 +168,15 @@
(if (< i 0)
frames
(let [frame-name (:name (get frames i))
tooling? (or (tooling-frame-name? frame-name)
;; Everything runs from a Thread, so this frame, if at
;; the end, is irrelevant. However one can invoke this
;; method 'by hand', which is why we only skip
;; consecutive frames that match this.
(and all-tooling-so-far?
(re-find #"^java\.lang\.Thread/run|^java\.util\.concurrent"
frame-name)))]
tooling? (and frame-name
(or (tooling-frame-name? frame-name)
;; Everything runs from a Thread, so this frame,
;; if at the end, is irrelevant. However one can
;; invoke this method 'by hand', which is why we
;; only skip consecutive frames that match this.
(and all-tooling-so-far?
(re-find #"^java\.lang\.Thread/run|^java\.util\.concurrent"
frame-name))))]
(recur (cond-> frames
tooling? (update i flag-frame :tooling))
(dec i) (and all-tooling-so-far? tooling?))))))
Expand Down Expand Up @@ -260,12 +263,13 @@
(print-fn % writer)
(str writer))
phase (-> cause-data :data :clojure.error/phase)
m {:class (name (:type cause-data))
:phase phase
:message (:message cause-data)
:stacktrace (analyze-stacktrace-data
(cond (seq (:trace cause-data)) (:trace cause-data)
(:at cause-data) [(:at cause-data)]))}]
m (-> {:class (name (:type cause-data))
:phase phase
:message (:message cause-data)
:stacktrace (analyze-stacktrace-data
(cond (seq (:trace cause-data)) (:trace cause-data)
(:at cause-data) [(:at cause-data)]))}
(assoc-some :triage (:triage cause-data)))]
(if-let [data (filter-ex-data (:data cause-data))]
(if (::s/failure data)
(assoc m
Expand All @@ -280,14 +284,28 @@
:clojure.error/symbol])))
m)))

(defn- maybe-triage-message
"If the exception is a compiler error which carries Spec-based explanation data,
transform it into human readable error message string."
[exception-data]
(try
;; ex-triage may throw an exception if :phase is incorrect
(when-let [explanation-data (:clojure.error/spec
(clojure.main/ex-triage exception-data))]
(with-out-str (s/explain-out explanation-data)))
(catch Exception _)))

(defn- analyze-causes
"Analyze the cause chain of the `exception-data` in `Throwable->map` format."
[exception-data print-fn]
(let [causes (vec (:via exception-data))
;; If the first cause lacks :trace, add :trace of the exception there.
causes (if (:trace (first causes))
causes
(assoc-in causes [0 :trace] (:trace exception-data)))]
(let [triage-message (maybe-triage-message exception-data)
causes (update (vec (:via exception-data)) 0
#(cond-> %
;; If the first cause lacks :trace, add :trace of the
;; exception there.
(nil? (:trace %)) (assoc :trace (:trace exception-data))
;; If non-nil, assoc triage-message to first cause.
triage-message (assoc :triage triage-message)))]
(mapv #(extract-location (analyze-cause % print-fn)) causes)))

(defn analyze
Expand Down
6 changes: 6 additions & 0 deletions test/orchard/stacktrace_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns orchard.stacktrace-test
(:require
[clojure.spec.alpha :as s]
[clojure.string :as str]
[clojure.test :refer [are deftest is testing]]
[matcher-combinators.matchers :as matchers]
[orchard.stacktrace :as sut]))
Expand Down Expand Up @@ -180,6 +181,11 @@
:clojure.error/symbol 'clojure.core/let}
(:location cause))))))

(deftest ex-triage-test
(testing "compilation errors that can be triaged contain :triage message"
(is (= "[a] - failed: even-number-of-forms? in: [0] at: [:bindings] spec: :clojure.core.specs.alpha/bindings"
(str/trim (:triage (first (catch-and-analyze (eval '(let [a]))))))))))

(deftest test-analyze-throwable
(testing "shape of analyzed throwable"
(is (match?
Expand Down