Skip to content

Commit 6c67655

Browse files
committed
read-ns-form: rethrow FileNotFoundExceptions more informatively
Fixes #142
1 parent b03efa4 commit 6c67655

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

CHANGELOG.md

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

55
* [#173](https://github.com/clojure-emacs/refactor-nrepl/issues/173): `rename-file-or-dir`: rename more kinds of constructs in dependent namespaces: namespace-qualified maps, fully-qualified functions, metadata.
66
* [#194](https://github.com/clojure-emacs/refactor-nrepl/issues/194): Don't prune `require` forms if they are needed for a given `import` to work.
7+
* [#142](https://github.com/clojure-emacs/refactor-nrepl/issues/142): `read-ns-form`: report more informatively when a non-existing file is being processed.
78

89
## 3.3.1
910

src/refactor_nrepl/core.clj

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
[refactor-nrepl.s-expressions :as sexp]
1111
[refactor-nrepl.util :as util :refer [normalize-to-unix-path]])
1212
(:import
13-
(java.io File FileReader PushbackReader StringReader)))
13+
(java.io File FileNotFoundException FileReader PushbackReader StringReader)))
1414

1515
;; Require our `fs` customizations before `fs` is loaded:
1616
(require '[refactor-nrepl.fs])
@@ -152,28 +152,29 @@
152152

153153
(defn read-ns-form
154154
([path]
155-
(let [^String path-string (when (string? path)
156-
path)
157-
^File path-file (when-not path-string
158-
path)]
159-
(with-open [file-reader (or (some-> path-string FileReader.)
160-
(some-> path-file FileReader.))]
161-
(try
162-
(parse/read-ns-decl (readers/indexing-push-back-reader
163-
(PushbackReader. file-reader)))
164-
(catch Exception _ nil)))))
155+
(read-ns-form nil path))
165156
([dialect path]
166157
(let [^String path-string (when (string? path)
167158
path)
168159
^File path-file (when-not path-string
169-
path)]
170-
(with-open [file-reader (or (some-> path-string FileReader.)
171-
(some-> path-file FileReader.))]
172-
(try
173-
(parse/read-ns-decl (readers/indexing-push-back-reader
174-
(PushbackReader. file-reader))
175-
{:read-cond :allow :features #{dialect}})
176-
(catch Exception _ nil))))))
160+
path)
161+
^File file (or path-file (File. path-string))]
162+
(try
163+
(with-open [file-reader (FileReader. file)]
164+
(try
165+
(parse/read-ns-decl (readers/indexing-push-back-reader
166+
(PushbackReader. file-reader))
167+
(if dialect
168+
{:read-cond :allow :features #{dialect}}
169+
nil))
170+
(catch Exception _ nil)))
171+
(catch FileNotFoundException e
172+
(throw (ex-info (format "No such file: %s. This typically indicates an invalid request client-side."
173+
(pr-str path))
174+
{:path path
175+
:dialect dialect
176+
:file (str file)}
177+
e)))))))
177178

178179
(defn cljc-extension? [^String path]
179180
(.endsWith path ".cljc"))

test/refactor_nrepl/core_test.clj

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@
2828
(assert-ignored-paths not-ignored false?)
2929
(assert-ignored-paths (concat always-ignored sometimes-ignored) true?)))))
3030

31-
(deftest test-read-ns-form
32-
(are [input expected] (testing input
33-
(assert (-> input File. .exists))
34-
(is (= expected
35-
(sut/read-ns-form input)))
36-
true)
37-
"test-resources/readable_file_incorrect_aliases.clj" nil
38-
"testproject/src/com/example/one.clj" '(ns com.example.one
39-
(:require [com.example.two :as two :refer [foo]]
40-
[com.example.four :as four]))))
31+
(deftest read-ns-form-test
32+
(let [valid-filename "testproject/src/com/example/one.clj"]
33+
(is (= (sut/read-ns-form valid-filename)
34+
(sut/read-ns-form :clj valid-filename)))
35+
(are [input expected] (testing input
36+
(assert (-> input File. .exists))
37+
(is (= expected
38+
(sut/read-ns-form input)))
39+
true)
40+
"test-resources/readable_file_incorrect_aliases.clj" nil
41+
valid-filename '(ns com.example.one
42+
(:require [com.example.two :as two :refer [foo]]
43+
[com.example.four :as four])))))
4144

4245
(deftest source-files-with-clj-like-extension-test
4346
(let [result (sut/source-files-with-clj-like-extension true)]

0 commit comments

Comments
 (0)