[#1105] Include native elements in printed stacktrace (#1113)

This commit is contained in:
Michiel Borkent 2021-12-22 13:04:29 +01:00 committed by GitHub
parent 697c03fa07
commit 86c67af49c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 6 deletions

@ -1 +1 @@
Subproject commit 7847bdd56bb8db3293070a5c09b94dddc77c0155
Subproject commit cc298e4a75e307f1265dcce11c629ea088423a9e

View file

@ -18,18 +18,18 @@
(drop (- stack-count 5) stacktrace)]))))
(defn print-stacktrace
[stacktrace {:keys [:verbose?]}]
[stacktrace {:keys [:debug]}]
(let [stacktrace (sci/format-stacktrace stacktrace)
segments (split-stacktrace stacktrace verbose?)
segments (split-stacktrace stacktrace debug)
[fst snd] segments]
(run! println fst)
(when snd
(println "...")
(println "... (run with --debug to see elided elements)")
(run! println snd))))
(defn error-context [ex opts]
(let [{:keys [:file :line :column]} (ex-data ex)]
(when (and file line)
(when (and file line column)
(when-let [content (case file
"<expr>" (:expression opts)
"<preloads>" (:preloads opts)
@ -81,6 +81,21 @@
(when (some :macro stacktrace)
"macroexpand")))
(defn render-native-sym [sym]
(let [sym (-> (str sym)
(clojure.lang.Compiler/demunge)
symbol)
ns (namespace sym)]
(when ns
(let [ns (symbol ns)
nm (symbol (name sym))]
{:ns ns
:name nm
:sci/built-in true}))))
(defn render-native-stacktrace-elem [[sym _ _file _line]]
(render-native-sym sym))
(defn error-handler [^Exception e opts]
(binding [*out* *err*]
(let [d (ex-data e)
@ -90,7 +105,12 @@
ex-name (when sci-error?
(some-> ^Throwable (ex-cause e)
.getClass .getName))
stacktrace (sci/stacktrace e)]
stacktrace (dedupe
(concat (sequence (comp (map StackTraceElement->vec)
(take-while #(not (str/starts-with? (first %) "sci.impl.")))
(map render-native-stacktrace-elem))
(.getStackTrace (or (ex-cause e) e)))
(sci/stacktrace e)))]
(if exit-code
(do
(when-let [m (.getMessage e)]

View file

@ -248,3 +248,10 @@ user - <expr>:1:45
----- Exception ----------------------------------------------------------------
clojure.lang.ExceptionInfo: null
{:type :sci/error, :line 1, :column 19,")))))
(deftest native-stacktrace-test
(let [output (try (tu/bb nil "(merge 1 2 3)")
(is false)
(catch Exception e (ex-message e)))]
(is (str/includes? (tu/normalize output)
"clojure.core/reduce1 - <built-in>"))))