Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d1de868

Browse filesBrowse files
committed
CLJ-2819 invoke-tool - improved error handling
1 parent 3895806 commit d1de868
Copy full SHA for d1de868

File tree

1 file changed

+36
-5
lines changed
Filter options

1 file changed

+36
-5
lines changed

‎src/clj/clojure/tools/deps/interop.clj

Copy file name to clipboardExpand all lines: src/clj/clojure/tools/deps/interop.clj
+36-5Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,34 @@
1010
(:require
1111
[clojure.java.process :as proc]
1212
[clojure.edn :as edn]
13-
[clojure.java.io :as jio]))
13+
[clojure.java.io :as jio]
14+
[clojure.string :as str]))
1415

1516
(set! *warn-on-reflection* true)
1617

18+
(def ^:private build (atom nil))
19+
20+
(defn- cli-build
21+
"Return CLI build number (a long) or nil if it can't be determined.
22+
The build number is cached if found and subsequently read from cache."
23+
[]
24+
(or @build
25+
(let [result (try
26+
(proc/exec "clojure" "--version")
27+
(catch Exception e ""))
28+
;; Version string: "Clojure CLI version 1.11.3.1463"
29+
;; Match MAJOR.MINOR.PATCH.BUILD and take a capture group just for the last BUILD part
30+
version (-> (re-find #"[0-9]+\.[0-9]+\.[0-9]+\.([0-9]+)" result) (nth 1))]
31+
(when version
32+
(reset! build (parse-long version))))))
33+
34+
(defn- validate-version
35+
[version]
36+
(if version
37+
(when (< version 1347)
38+
(throw (RuntimeException. "Clojure CLI version is older than minimum required version, 1.11.1.1347. Please update to latest version.")))
39+
(throw (ex-info "Clojure CLI version unknown, please install the latest version." {}))))
40+
1741
(defn ^:dynamic invoke-tool
1842
"Invoke tool using Clojure CLI. Args (one of :tool-alias or :tool-name, and :fn
1943
are required):
@@ -29,14 +53,16 @@
2953
:or {preserve-envelope false}
3054
:as opts}]
3155
(when-not (or tool-name tool-alias) (throw (ex-info "Either :tool-alias or :tool-name must be provided" (or opts {}))))
32-
(when-not (symbol? fn) (throw (ex-info (str "fn should be a symbol " fn) (or opts {}))))
56+
(when-not (symbol? fn) (throw (ex-info (str ":fn should be a symbol " fn) (or opts {}))))
57+
(validate-version (cli-build))
3358
(let [args (conj [fn] (assoc args :clojure.exec/invoke :fn))
3459
_ (when (:debug opts) (println "args" args))
3560
command-strs ["clojure" (str "-T" (or tool-alias tool-name)) "-"]
3661
_ (when (:debug opts) (apply println "Invoking: " command-strs))
3762
proc (apply proc/start command-strs)
3863
in (proc/stdin proc)
39-
out (proc/stdout proc)]
64+
out (proc/stdout proc)
65+
err (proc/stderr proc)]
4066
(binding [*print-length* nil
4167
*print-level* nil
4268
*print-namespace-maps* false]
@@ -45,14 +71,19 @@
4571
(doseq [a args]
4672
(.write w (pr-str a))
4773
(.write w " ")))))
48-
(let [envelope (edn/read-string (slurp out))]
74+
(if-let [envelope (edn/read-string (slurp out))]
4975
(if preserve-envelope
5076
envelope
5177
(let [{:keys [tag val]} envelope
5278
parsed-val (edn/read-string val)]
5379
(if (= :ret tag)
5480
parsed-val
55-
(throw (ex-info (:cause parsed-val) (or parsed-val {})))))))))
81+
(throw (ex-info (:cause parsed-val) (or parsed-val {}))))))
82+
(let [err-str (slurp err)
83+
err-msg (if (= "" err-str) "Unknown error invoking Clojure CLI" err-str)]
84+
(throw (ex-info err-msg
85+
{:command (str/join " " command-strs)
86+
:in (str/join " " args)}))))))
5687

5788
(comment
5889
;; regular invocation, should return {:hi :there}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.