Skip to content

Commit 40d34c5

Browse files
author
dnolen
committed
CLJS-1057: Nashorn REPL should not use EDN rep for errors
Clean up error handling in general. Giving functions top level names is very noisy and introduces incredible complexity with respect to function name printing, every engine is different.
1 parent d7fc5de commit 40d34c5

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

src/clj/cljs/repl.clj

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@
109109
IReplEnvOptions
110110
(-repl-options [_] nil))
111111

112-
(defprotocol IParseErrorMessage
113-
(-parse-error-message [repl-env message error build-options]
114-
"Given the original JavaScript error message return the string to actually
112+
(defprotocol IParseError
113+
(-parse-error [repl-env error build-options]
114+
"Given the original JavaScript error return the error to actually
115115
use."))
116116

117117
(defprotocol IGetError
@@ -271,8 +271,7 @@
271271
[line' column'] (if ns-info
272272
(mapped-line-and-column sm line column)
273273
[line column])
274-
name' (if (and ns-info function)
275-
(symbol (name ns) (cljrepl/demunge function))
274+
name' (when (and ns-info function)
276275
function)
277276
file' (if no-source-file?
278277
file
@@ -994,20 +993,16 @@ str-or-pattern."
994993
([e]
995994
(let [{:keys [repl-env] :as env} &env]
996995
(when (and e repl-env)
997-
(let [ret (if (satisfies? IGetError repl-env)
996+
(when-let [ret (if (satisfies? IGetError repl-env)
998997
(-get-error repl-env e env *repl-opts*)
999998
(edn/read-string
1000999
(evaluate-form repl-env env "<cljs repl>"
10011000
`(when ~e
10021001
(pr-str
10031002
{:value (.-message ~e)
10041003
:stacktrace (.-stack ~e)})))))]
1005-
(when ret
1006-
(let [ret (update-in ret [:value]
1007-
(fn [msg]
1008-
;; give REPL environments a chance to fix or
1009-
;; or elide redundant information
1010-
(if (satisfies? IParseErrorMessage repl-env)
1011-
(-parse-error-message repl-env msg ret *repl-opts*)
1012-
msg)))]
1013-
(display-error repl-env ret nil *repl-opts*))))))))
1004+
(display-error repl-env
1005+
(if (satisfies? IParseError repl-env)
1006+
(-parse-error repl-env ret *repl-opts*)
1007+
ret)
1008+
nil *repl-opts*))))))

src/clj/cljs/repl/nashorn.clj

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
[cljs.repl :as repl]
1616
[cljs.compiler :as comp]
1717
[cljs.closure :as closure])
18-
(:import [javax.script ScriptEngine ScriptEngineManager ScriptException ScriptEngineFactory]
18+
(:import [java.io File]
19+
[javax.script ScriptEngine ScriptEngineManager ScriptException ScriptEngineFactory]
1920
[jdk.nashorn.api.scripting NashornException]))
2021

2122
;; Nashorn Clojurescript repl binding.
@@ -141,12 +142,6 @@
141142
(string/replace-first file-name with-slash "")
142143
file-name)))
143144

144-
(defn- convert-stacktrace-element [^StackTraceElement el]
145-
{:function (.getMethodName el)
146-
:file (.getFileName el)
147-
:line (.getLineNumber el)
148-
:column 0})
149-
150145
(def repl-filename "<cljs repl>")
151146

152147
(defrecord NashornEnv [engine debug]
@@ -183,26 +178,43 @@
183178
(try
184179
{:status :success
185180
:value (if-let [r (eval-str engine js)] (.toString r) "")}
186-
187-
;; Stringify the stacktrace to edn for easy parsing in -parse-stacktrace
188181
(catch ScriptException e
189182
(let [^Throwable root-cause (clojure.stacktrace/root-cause e)]
190183
{:status :exception
191184
:value (.getMessage root-cause)
192-
:stacktrace (pr-str (map convert-stacktrace-element
193-
(NashornException/getScriptFrames root-cause)))}))
185+
:stacktrace (NashornException/getScriptStackString root-cause)}))
194186
(catch Throwable e
195187
(let [^Throwable root-cause (clojure.stacktrace/root-cause e)]
196188
{:status :exception
197189
:value (.getMessage root-cause)
198-
:stacktrace (pr-str (map convert-stacktrace-element (.getStackTrace root-cause)))}))))
190+
:stacktrace
191+
(apply str
192+
(interpose "\n"
193+
(map str
194+
(.getStackTrace root-cause))))}))))
199195
(-load [{engine :engine :as this} ns url]
200196
(load-ns engine ns))
201197
(-tear-down [this])
202198
repl/IParseStacktrace
203199
(-parse-stacktrace [this frames-str ret {output-dir :output-dir}]
204-
(when-let [frames (read-string frames-str)]
205-
(vec (map #(update-in %1 [:file] (fn [s] (strip-file-name s output-dir))) frames)))))
200+
(vec
201+
(map
202+
(fn [frame-str]
203+
(let [frame-str (string/replace frame-str #"\s+at\s+" "")
204+
[function file-and-line] (string/split frame-str #"\s+")
205+
[file-part line-part] (string/split file-and-line #":")]
206+
{:file (string/replace (.substring file-part 1)
207+
(str output-dir File/separator) "")
208+
:function function
209+
:line (Integer/parseInt
210+
(.substring line-part 0 (dec (.length line-part))))
211+
:column 0}))
212+
(string/split frames-str #"\n"))))
213+
repl/IParseError
214+
(-parse-error [_ err _]
215+
(update-in err [:stacktrace]
216+
(fn [st]
217+
(string/join "\n" (drop 1 (string/split st #"\n")))))))
206218

207219
(defn repl-env* [{:keys [debug] :as opts}]
208220
(let [engine (create-engine opts)]

src/clj/cljs/repl/node.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@
173173
(-repl-options [this]
174174
{:output-dir ".cljs_node_repl"
175175
:target :nodejs})
176-
repl/IParseErrorMessage
177-
(-parse-error-message [_ _ _ _])
176+
repl/IParseError
177+
(-parse-error [_ err _]
178+
(assoc err :value nil))
178179
repl/IJavaScriptEnv
179180
(-setup [this opts]
180181
(setup this opts))

0 commit comments

Comments
 (0)