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
This commit is contained in:
Stel Abrego 2022-04-05 02:03:39 -07:00 committed by GitHub
parent 60df83b629
commit d2278835f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 1 deletions

View file

@ -5,6 +5,10 @@ For a list of breaking changes, check [here](#breaking-changes).
A preview of the next release can be installed from A preview of the next release can be installed from
[babashka-dev-builds](https://github.com/babashka/babashka-dev-builds). [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) ## 0.8.0 (2022-04-04)
### New ### New

View file

@ -964,6 +964,13 @@ Use bb run --help to show this help output.
(and (= minor-current minor-min) (and (= minor-current minor-min)
(>= patch-current patch-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] (defn main [& args]
(let [[args global-opts] (parse-global-opts args) (let [[args global-opts] (parse-global-opts args)
{:keys [:jar] :as file-opt} (when (some-> args first io/file .isFile) {: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 bb-edn (when bb-edn-file
(System/setProperty "babashka.config" bb-edn-file) (System/setProperty "babashka.config" bb-edn-file)
(let [raw-string (slurp bb-edn-file) (let [raw-string (slurp bb-edn-file)
edn (edn/read-string raw-string) edn (load-edn raw-string)
edn (assoc edn edn (assoc edn
:raw raw-string :raw raw-string
:file bb-edn-file) :file bb-edn-file)

View file

@ -200,6 +200,12 @@
(is (thrown-with-msg? (is (thrown-with-msg?
Exception #"Cyclic task: b" Exception #"Cyclic task: b"
(bb "run" "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" (testing "doc"
(test-utils/with-config '{:tasks {b {:doc "Beautiful docstring"}}} (test-utils/with-config '{:tasks {b {:doc "Beautiful docstring"}}}
(let [s (test-utils/bb nil "doc" "b")] (let [s (test-utils/bb nil "doc" "b")]