Skip to content

Commit

Permalink
Imitate clojure.main Error Reporting
Browse files Browse the repository at this point in the history
Fixes #1004
  • Loading branch information
mfikes committed Aug 17, 2019
1 parent d757c20 commit b123fb9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
6 changes: 1 addition & 5 deletions planck-cljs/src/planck/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,7 @@
"Resolves namespace-qualified sym per [[resolve]]. If initial resolve
fails, attempts to require `sym`'s namespace and retries."
[sym]
(if (qualified-symbol? sym)
(or (resolve sym)
(do (eval `(require '~(-> sym namespace symbol)))
(resolve sym)))
(throw (js/Error. (str "Not a qualified symbol: " sym)))))
(#'repl/requiring-resolve sym))

(s/fdef requiring-resolve
:args (s/cat :sym qualified-symbol?)
Expand Down
35 changes: 35 additions & 0 deletions planck-cljs/src/planck/from/clojure/main.cljs
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)]
(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)))))))
19 changes: 15 additions & 4 deletions planck-cljs/src/planck/repl.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,13 +1222,16 @@
(load m (load-opts) cb))

(declare ^{:arglists '([error])} skip-cljsjs-eval-error)
(declare ^{:arglists '([sym])} requiring-resolve)

(defn- handle-error
[e include-stacktrace?]
(do
(print-error e include-stacktrace?)
(if (not (:repl @app-env))
(js/PLANCK_EXIT_WITH_VALUE 1)
(if (not (:repl @app-env))
(do
((requiring-resolve 'planck.from.clojure.main/report-error) e)
(js/PLANCK_EXIT_WITH_VALUE 1))
(do
(print-error e include-stacktrace?)
(set! *e (skip-cljsjs-eval-error e)))))

(defn- get-eval-fn []
Expand Down Expand Up @@ -2198,6 +2201,14 @@
[sym]
(ns-resolve (.-name *ns*) sym))

(defn- requiring-resolve
[sym]
(if (qualified-symbol? sym)
(or (resolve sym)
(do (cljs.core/eval `(require '~(-> sym namespace symbol)))
(resolve sym)))
(throw (js/Error. (str "Not a qualified symbol: " sym)))))

(defn get-arglists
"Return the argument lists for the given symbol as string, or nil if not
found."
Expand Down

0 comments on commit b123fb9

Please # to comment.