From d2278835f1935cd53548022a636f60d5282e0d23 Mon Sep 17 00:00:00 2001 From: Stel Abrego Date: Tue, 5 Apr 2022 02:03:39 -0700 Subject: [PATCH] Fix #1041: Improve error message when regex literal in EDN config (#1230) * Improve error message when regex literal in EDN config (#1041) Regex literal syntax (example: #"^foo") is not allowed in EDN. With the ability to write tasks in the config file, users may mistakenly try to include regex literals. This patch improves the error message by informing the user that literal regex syntax is not allowed and recommends using re-pattern instead. * Update changelog * Fixup main.clj * Fixup changelog --- CHANGELOG.md | 4 ++++ src/babashka/main.clj | 9 ++++++++- test/babashka/bb_edn_test.clj | 6 ++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ed4f2ba..6fe55d00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ For a list of breaking changes, check [here](#breaking-changes). A preview of the next release can be installed from [babashka-dev-builds](https://github.com/babashka/babashka-dev-builds). +## 0.8.1 (TBD) + +- [#1041](https://github.com/babashka/babashka/issues/1041): Improve error message when regex literal in EDN config + ## 0.8.0 (2022-04-04) ### New diff --git a/src/babashka/main.clj b/src/babashka/main.clj index c33473ae..268985a0 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -964,6 +964,13 @@ Use bb run --help to show this help output. (and (= minor-current minor-min) (>= patch-current patch-min))))))) +(defn load-edn [string] + (try (edn/read-string string) + (catch java.lang.RuntimeException e + (if (re-find #"No dispatch macro for: \"" (.getMessage e)) + (throw (ex-info "Invalid regex literal found in EDN config, use re-pattern instead" {})) + (throw e))))) + (defn main [& args] (let [[args global-opts] (parse-global-opts args) {:keys [:jar] :as file-opt} (when (some-> args first io/file .isFile) @@ -977,7 +984,7 @@ Use bb run --help to show this help output. bb-edn (when bb-edn-file (System/setProperty "babashka.config" bb-edn-file) (let [raw-string (slurp bb-edn-file) - edn (edn/read-string raw-string) + edn (load-edn raw-string) edn (assoc edn :raw raw-string :file bb-edn-file) diff --git a/test/babashka/bb_edn_test.clj b/test/babashka/bb_edn_test.clj index 69a3a7a7..30a9bac2 100644 --- a/test/babashka/bb_edn_test.clj +++ b/test/babashka/bb_edn_test.clj @@ -200,6 +200,12 @@ (is (thrown-with-msg? Exception #"Cyclic task: b" (bb "run" "b"))))) + (testing "friendly regex literal error handling" + (test-utils/with-config + "{:tasks {something (clojure.string/split \"1-2\" #\"-\")}}" + (is (thrown-with-msg? + Exception #"Invalid regex literal" + (bb "run" "something"))))) (testing "doc" (test-utils/with-config '{:tasks {b {:doc "Beautiful docstring"}}} (let [s (test-utils/bb nil "doc" "b")]