error message for (clojure.java.io/reader nil) — consistency for use with io/resource

Description

This seems to be a common idiom:

(clojure.java.io/reader (clojure.java.io/resource "myfile"))

When a file is available these are the behaviors:

=> (clojure.java.io/reader "resources/myfile") #<BufferedReader java.io.BufferedReader@1f291df0> => (clojure.java.io/resource "myfile") #<URL file:/project/resources/myfile> => (clojure.java.io/reader (clojure.java.io/resource "myfile")) #<BufferedReader java.io.BufferedReader@1db04f7c>

If the file (resource) is unavailable:

=> (clojure.java.io/reader "resources/nofile") FileNotFoundException resources/nofile (No such file or directory) java.io.FileInputStream.open (FileInputStream.java:-2) => (clojure.java.io/resource "nofile") nil => (clojure.java.io/reader (clojure.java.io/resource "nofile")) IllegalArgumentException No implementation of method: :make-reader of protocol: #'clojure.java.io/IOFactory found for class: nil clojure.core/-cache-protocol-fn (core_deftype.clj:541) ;; EXPECTED: better error type/message

This situation would be improved with a better error message from `(clojure.java.io/reader nil)`.

Approach: Extend IOFactory to nil, providing error messages consistent with the default error messages provided for Object.

After:

user=> (clojure.java.io/reader (clojure.java.io/resource "nofile")) IllegalArgumentException Cannot open <nil> as a Reader. clojure.java.io/fn--9213 (io.clj:290)

Patch: clj-1210-2.patch

Environment

None

Attachments

2
  • 09 Oct 2015, 03:03 PM
  • 14 Feb 2014, 09:13 PM

Activity

Show:

Benjamin Peter October 13, 2015 at 9:09 AM

Thanks a lot.

Alex Miller October 9, 2015 at 3:03 PM

Added -2 patch that makes commit message conform to expectations, no other changes, attribution retained.

import July 12, 2014 at 8:01 AM

Comment made by: devurandom

I would also be interested in a solution, as I am currently running into this with the ClojureScript compiler.

Benjamin Peter February 15, 2014 at 7:31 PM

Looks like a good solution to me as a user. Thanks for the effort!

Alexander Redington February 14, 2014 at 9:13 PM

This patch extends IOFactory to nil, providing error messages consistent with the default error messages provided for Object.

Completed

Details

Assignee

Reporter

Approval

Ok

Patch

Code and Test

Priority

Affects versions

Fix versions

Created May 23, 2013 at 11:59 PM
Updated October 13, 2015 at 9:09 AM
Resolved October 13, 2015 at 9:09 AM