Clojure

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

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: Release 1.5
  • Fix Version/s: None
  • Component/s: None
  • Patch:
    Code and Test
  • Approval:
    Triaged

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

The main enhancement request is to have 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: extend-io-factory-to-nil.diff

Activity

Alex Miller made changes -
Field Original Value New Value
Labels errormsgs
Alex Miller made changes -
Labels errormsgs errormsgs io
Alexander Redington made changes -
Attachment extend-io-factory-to-nil.diff [ 12796 ]
Alexander Redington made changes -
Patch Code and Test [ 10002 ]
Alex Miller made changes -
Approval Triaged [ 10120 ]
Alex Miller made changes -
Priority Minor [ 4 ] Major [ 3 ]
Alex Miller made changes -
Description This seems to 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)


The main enhancement request is to have a better error message from `(clojure.java.io/reader nil)`. I'm not sure if io/resource should return something like 'resource "nofile" not found' or if io/reader could add a more helpful suggestion.
This seems to be a common idiom:

{code}
(clojure.java.io/reader (clojure.java.io/resource "myfile"))
{code}


When a file is available these are the behaviors:

{code}
=> (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>
{code}

If the file (resource) is unavailable:

{code}
=> (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
{code}

The main enhancement request is to have 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:

{code}
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)
{code}

*Patch:* extend-io-factory-to-nil.diff
Labels errormsgs io errormsgs ft io
Andy Fingerhut made changes -
Labels errormsgs ft io checkargs errormsgs ft io

People

Vote (4)
Watch (5)

Dates

  • Created:
    Updated: