From 024f1d55154404d627875c088b08ed8b57b42179 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 20 Dec 2024 17:20:45 +0100 Subject: [PATCH] Fix #1771: *e should contain exception thrown by user (#1784) --- CHANGELOG.md | 9 +++++---- src/babashka/impl/clojure/main.clj | 5 +++-- src/babashka/impl/repl.clj | 2 +- test/babashka/impl/repl_test.clj | 22 ++++++++++++---------- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66781aa1..ca3f0099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,10 +9,11 @@ A preview of the next release can be installed from ## Unreleased -- [#1777] Add java.nio.file.attribute.UserDefinedFileAttributeView -- [#1776] Add java.nio.file.attribute.PosixFileAttributes -- [#1761] Support calling `clojure.lang.RT/iter` -- [#1760] For compatibility with [Fireworks v0.10.3](https://github.com/paintparty/fireworks), added the following to `:instance-checks` entry in `babashka.impl.classes/classes`([@paintparty](https://github.com/paintparty)) +- [#1771](https://github.com/babashka/babashka/issues/1771): `*e*` in REPL should contain exception thrown by user, not a wrapped one +- [#1777](https://github.com/babashka/babashka/issues/1777) Add java.nio.file.attribute.UserDefinedFileAttributeView +- [#1776](https://github.com/babashka/babashka/issues/1776) Add java.nio.file.attribute.PosixFileAttributes +- [#1761](https://github.com/babashka/babashka/issues/1761) Support calling `clojure.lang.RT/iter` +- [#1760](https://github.com/babashka/babashka/issues/1760) For compatibility with [Fireworks v0.10.3](https://github.com/paintparty/fireworks), added the following to `:instance-checks` entry in `babashka.impl.classes/classes`([@paintparty](https://github.com/paintparty)) - `clojure.lang.PersistentArrayMap$TransientArrayMap` - `clojure.lang.PersistentHashMap$TransientHashMap` - `clojure.lang.PersistentVector$TransientVector` diff --git a/src/babashka/impl/clojure/main.clj b/src/babashka/impl/clojure/main.clj index af89f966..d359cfa4 100644 --- a/src/babashka/impl/clojure/main.clj +++ b/src/babashka/impl/clojure/main.clj @@ -118,8 +118,9 @@ by default when a new command-line REPL is started."} repl-requires :file "" :type :sci/error) e))))))) (catch Throwable e - (caught e) - (set! *e e))))] + (let [e' (ex-cause e)] + (caught e) + (set! *e e')))))] (with-bindings (try (init) diff --git a/src/babashka/impl/repl.clj b/src/babashka/impl/repl.clj index 289ba1ec..e167e649 100644 --- a/src/babashka/impl/repl.clj +++ b/src/babashka/impl/repl.clj @@ -43,7 +43,7 @@ "Inspired by skip-if-eol from clojure.main." [s] (let [c (r/read-char s)] - (when-not (= c \newline) + (when-not (= \newline c ) (r/unread s c)))) (defn repl-read [sci-ctx in-stream _request-prompt request-exit] diff --git a/test/babashka/impl/repl_test.clj b/test/babashka/impl/repl_test.clj index 49cdab82..faa6f4c5 100644 --- a/test/babashka/impl/repl_test.clj +++ b/test/babashka/impl/repl_test.clj @@ -26,15 +26,15 @@ (sci/with-in-str (str expr "\n:repl/quit") (repl!)))) expected))) -(defn assert-repl-error [expr expected] - (is (str/includes? - (tu/normalize - (let [sw (java.io.StringWriter.)] - (sci/binding [sci/out (java.io.StringWriter.) - sci/err sw] - (sci/with-in-str (str expr "\n:repl/quit") - (repl!))) - (str sw))) expected))) +(defmacro assert-repl-error [expr expected] + `(is (str/includes? + (tu/normalize + (let [sw# (java.io.StringWriter.)] + (sci/binding [sci/out (java.io.StringWriter.) + sci/err sw#] + (sci/with-in-str (str ~expr "\n:repl/quit") + (repl!))) + (str sw#))) ~expected))) (deftest repl-test (assert-repl "1" "1") @@ -52,7 +52,9 @@ (assert-repl-error "(+ 1 nil)" "NullPointerException") (assert-repl-error "(/ 1 0) (pst 1)" "Divide by zero\n\tclojure.lang.Numbers") (assert-repl-error "(partition (range 5) 3)" - "Don't know how to create ISeq from: java.lang.Long")) + "Don't know how to create ISeq from: java.lang.Long") + (assert-repl "(throw (ex-info \"foo\" {:a (+ 1 2 3)})) (ex-data *e)" + "{:a 6}")) ;;;; Scratch