Skip to content

Commit 8f8647d

Browse files
andersenleobenedekfazekas
authored andcommitted
Ignore parsing error when reading artifacts list from clojars (#256)
* Add try/catch when reading EDN data Ignores unreadable entities. * Move edn-read to 'safe' function and add unit-test * Add changelog entry
1 parent 9e2a0cc commit 8f8647d

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* [#251](https://github.com/clojure-emacs/refactor-nrepl/pull/251) `clean-ns` support extra message key `relative-path`, which will be used if `path` does not exist.
6+
* [#256](https://github.com/clojure-emacs/refactor-nrepl/pull/256) ignore malformed artifact coordinates when fetching from Clojars.
67

78
## 2.4.0
89

src/refactor_nrepl/artifacts.clj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
(neg? (- millis-per-day (- (.getTime (java.util.Date.)) last-modified)))
3232
true)))
3333

34+
(defn- edn-read-or-nil
35+
"Read a form `s`. Return nil if it cannot be parsed."
36+
[s]
37+
(try (edn/read-string s)
38+
(catch Exception _
39+
;; Ignore artifact if not readable. See #255
40+
nil)))
41+
3442
(defn get-clojars-artifacts!
3543
"Returns a vector of [[some/lib \"0.1\"]...]."
3644
[]
@@ -39,7 +47,7 @@
3947
java.net.URL.
4048
io/reader
4149
line-seq
42-
(map edn/read-string))
50+
(keep edn-read-or-nil))
4351
(catch Exception _
4452
;; In the event clojars is down just return an empty vector. See #136.
4553
[])))

test/refactor_nrepl/artifacts_test.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,10 @@
4646
(reset! artifacts/artifacts {"org.clojure/clojure" clojure-versions})
4747
(is (= sorted-clojure-versions
4848
(artifacts/artifact-versions {:artifact "org.clojure/clojure"}))))))
49+
50+
(deftest ignores-invalid-artifact-forms
51+
(let [bad-form "[bad/1.1 \"funky\"]"
52+
good-form "[foo/bar \"1.1\"]"]
53+
(is (nil? (#'artifacts/edn-read-or-nil bad-form)))
54+
(is (= 'foo/bar (first (#'artifacts/edn-read-or-nil good-form))))
55+
(is (= "1.1" (second (#'artifacts/edn-read-or-nil good-form))))))

0 commit comments

Comments
 (0)