Clojure

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

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Completed
  • Affects Version/s: Release 1.5
  • Fix Version/s: Release 1.8
  • Component/s: None
  • Patch:
    Code and Test
  • Approval:
    Ok

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

  1. extend-io-factory-to-nil.diff
    14/Feb/14 3:13 PM
    2 kB
    Alexander Redington
  2. clj-1210-2.patch
    09/Oct/15 9:03 AM
    2 kB
    Alex Miller

Activity

Hide
Alexander Redington added a comment -

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

Show
Alexander Redington added a comment - This patch extends IOFactory to nil, providing error messages consistent with the default error messages provided for Object.
Hide
Benjamin Peter added a comment - - edited

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

Show
Benjamin Peter added a comment - - edited Looks like a good solution to me as a user. Thanks for the effort!
Hide
Dennis Schridde added a comment -

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

Show
Dennis Schridde added a comment - I would also be interested in a solution, as I am currently running into this with the ClojureScript compiler.
Hide
Alex Miller added a comment -

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

Show
Alex Miller added a comment - Added -2 patch that makes commit message conform to expectations, no other changes, attribution retained.
Hide
Benjamin Peter added a comment -

Thanks a lot.

Show
Benjamin Peter added a comment - Thanks a lot.

People

Vote (4)
Watch (5)

Dates

  • Created:
    Updated:
    Resolved: