-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Imitate clojure.main Error Reporting
Fixes #1004
- Loading branch information
Showing
11 changed files
with
234 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
(ns planck.from.cljs.repl) | ||
|
||
(def ^:dynamic *extract-canonical-stacktrace* (fn [_] [])) | ||
|
||
;; Copied and made to use *extract-canonical-stacktrace* | ||
(defn Error->map | ||
"Constructs a data representation for a Error with keys: | ||
:cause - root cause message | ||
:phase - error phase | ||
:via - cause chain, with cause keys: | ||
:type - exception class symbol | ||
:message - exception message | ||
:data - ex-data | ||
:at - top stack element | ||
:trace - root cause stack elements" | ||
[o] | ||
(let [base (fn [t] | ||
(merge {:type (cond | ||
(instance? ExceptionInfo t) 'ExceptionInfo | ||
(instance? js/EvalError t) 'js/EvalError | ||
(instance? js/RangeError t) 'js/RangeError | ||
(instance? js/ReferenceError t) 'js/ReferenceError | ||
(instance? js/SyntaxError t) 'js/SyntaxError | ||
(instance? js/URIError t) 'js/URIError | ||
(instance? js/Error t) 'js/Error | ||
:else nil)} | ||
(when-let [msg (ex-message t)] | ||
{:message msg}) | ||
(when-let [ed (ex-data t)] | ||
{:data ed}) | ||
(let [st (*extract-canonical-stacktrace* t)] | ||
(when (pos? (count st)) | ||
{:at st})))) | ||
via (loop [via [], t o] | ||
(if t | ||
(recur (conj via t) (ex-cause t)) | ||
via)) | ||
root (peek via)] | ||
(merge {:via (vec (map base via)) | ||
:trace (*extract-canonical-stacktrace* (or root o))} | ||
(when-let [root-msg (ex-message root)] | ||
{:cause root-msg}) | ||
(when-let [data (ex-data root)] | ||
{:data data}) | ||
(when-let [phase (-> o ex-data :clojure.error/phase)] | ||
{:phase phase})))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(ns ^:no-doc planck.from.clojure.main | ||
(:require | ||
[planck.core :refer [requiring-resolve with-open spit]] | ||
[planck.io :as io :refer [temp-file]] | ||
[cljs.repl])) | ||
|
||
;; Copied and revised for use in Planck | ||
(defn report-error | ||
"Create and output an exception report for a Throwable to target. | ||
Options: | ||
:target - \"file\" (default), \"stderr\", \"none\" | ||
If file is specified but cannot be written, falls back to stderr." | ||
[t & {:keys [target] | ||
:or {target "file"} :as opts}] | ||
(when-not (= target "none") | ||
(let [trace (cljs.repl/Error->map t) | ||
triage (cljs.repl/ex-triage trace) | ||
message (cljs.repl/ex-str triage) | ||
report (array-map | ||
:planck.repl/message message | ||
:planck.repl/triage triage | ||
:planck.repl/trace trace) | ||
report-str (with-out-str | ||
(binding [*print-namespace-maps* false] | ||
((requiring-resolve 'clojure.pprint/pprint) report))) | ||
err-path (when (= target "file") | ||
(try | ||
(let [f (temp-file "planck-" ".edn")] | ||
(spit f report-str) | ||
(:path f)) | ||
(catch :default _)))] ;; ignore, fallback to stderr | ||
(binding [*print-fn* *print-err-fn*] | ||
(if err-path | ||
(println (str message \newline "Full report at:" \newline err-path)) | ||
(println (str report-str \newline message))))))) |
Oops, something went wrong.