diff --git a/src/babashka/impl/repl.clj b/src/babashka/impl/repl.clj index 467c9e79..ae0c8b4f 100644 --- a/src/babashka/impl/repl.clj +++ b/src/babashka/impl/repl.clj @@ -17,14 +17,25 @@ "Default :caught hook for repl" [^Throwable e] (sci/with-bindings {sci/out @sci/err} - (let [d (ex-data e) + (let [{:keys [:file :line :column] :as d} (ex-data e) sci-error? (identical? :sci/error (:type d)) ex-name (when sci-error? (some-> ^Throwable (ex-cause e) - .getClass .getName))] + .getClass .getName)) + ex-message (when-let [m (.getMessage e)] + (when-not (str/blank? m) + m))] (sio/println (str ex-name - (when-let [m (.getMessage e)] - (str ": " m)) )) + (when ex-message + (str (when ex-name ": ") + ex-message)) + (when file + (str + (when (or ex-name ex-message) + " ") + "[at " file + (when line + (str ":" line ":" column))"]")))) (sio/flush)))) (defn repl @@ -51,18 +62,19 @@ v)))) :eval (or eval (fn [expr] - (let [ret (eval-form (update sci-ctx - :env - (fn [env] - (swap! env update-in [:namespaces 'clojure.core] - assoc - '*1 *1 - '*2 *2 - '*3 *3 - '*e *e) - env)) - expr)] - ret))) + (sci/with-bindings {sci/file ""} + (let [ret (eval-form (update sci-ctx + :env + (fn [env] + (swap! env update-in [:namespaces 'clojure.core] + assoc + '*1 *1 + '*2 *2 + '*3 *3 + '*e *e) + env)) + expr)] + ret)))) :need-prompt (or need-prompt (fn [] true)) :prompt (or prompt #(sio/printf "%s=> " (vars/current-ns-name))) :flush (or flush sio/flush) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 6d3ec7ac..11303f94 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -541,7 +541,11 @@ (deftest repl-test (is (str/includes? (test-utils/bb "(ns foo) ::foo" "--repl") ":foo/foo")) (is (str/includes? (test-utils/bb "[*warn-on-reflection* (set! *warn-on-reflection* true) *warn-on-reflection*]") - "[false true true]"))) + "[false true true]")) + (let [sw (java.io.StringWriter.)] + (sci/with-bindings {sci/err sw} + (test-utils/bb {:in "x" :err sw} "--repl")) + (is (str/includes? (str sw) "Could not resolve symbol: x [at :1:1]")))) ;;;; Scratch diff --git a/test/babashka/test_utils.clj b/test/babashka/test_utils.clj index b4dcefd9..c4719deb 100644 --- a/test/babashka/test_utils.clj +++ b/test/babashka/test_utils.clj @@ -12,8 +12,10 @@ (let [os (java.io.StringWriter.) es (if-let [err (:err input-or-opts)] err (java.io.StringWriter.)) - is (when (string? input-or-opts) - (java.io.StringReader. input-or-opts)) + in (if (string? input-or-opts) + input-or-opts (:in input-or-opts)) + is (when in + (java.io.StringReader. in)) bindings-map (cond-> {sci/out os sci/err es} is (assoc sci/in is))]