Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try/catch when reading EDN data #256

Merged
merged 3 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* [#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.
* [#256](https://github.com/clojure-emacs/refactor-nrepl/pull/256) ignore malformed artifact coordinates when fetching from Clojars.

## 2.4.0

Expand Down
10 changes: 9 additions & 1 deletion src/refactor_nrepl/artifacts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
(neg? (- millis-per-day (- (.getTime (java.util.Date.)) last-modified)))
true)))

(defn- edn-read-or-nil
"Read a form `s`. Return nil if it cannot be parsed."
[s]
(try (edn/read-string s)
(catch Exception _
;; Ignore artifact if not readable. See #255
nil)))

(defn get-clojars-artifacts!
"Returns a vector of [[some/lib \"0.1\"]...]."
[]
Expand All @@ -39,7 +47,7 @@
java.net.URL.
io/reader
line-seq
(map edn/read-string))
(keep edn-read-or-nil))
(catch Exception _
;; In the event clojars is down just return an empty vector. See #136.
[])))
Expand Down
7 changes: 7 additions & 0 deletions test/refactor_nrepl/artifacts_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@
(reset! artifacts/artifacts {"org.clojure/clojure" clojure-versions})
(is (= sorted-clojure-versions
(artifacts/artifact-versions {:artifact "org.clojure/clojure"}))))))

(deftest ignores-invalid-artifact-forms
(let [bad-form "[bad/1.1 \"funky\"]"
good-form "[foo/bar \"1.1\"]"]
(is (nil? (#'artifacts/edn-read-or-nil bad-form)))
(is (= 'foo/bar (first (#'artifacts/edn-read-or-nil good-form))))
(is (= "1.1" (second (#'artifacts/edn-read-or-nil good-form))))))