33
44module JSON
55 class Schema
6- # Raised by {JSON::Schema::Reader} when one of its settings indicate
7- # a schema should not be readed.
8- class ReadRefused < StandardError
6+ # Base for any reading exceptions encountered by {JSON::Schema::Reader}
7+ class ReadError < StandardError
98 # @return [String] the requested schema location which was refused
109 attr_reader :location
1110
@@ -15,7 +14,30 @@ class ReadRefused < StandardError
1514 def initialize ( location , type )
1615 @location = location
1716 @type = type
18- super ( "Read of #{ type == :uri ? 'URI' : type } at #{ location } refused!" )
17+ super ( error_message )
18+ end
19+
20+ private
21+
22+ def type_string
23+ type == :uri ? 'URI' : type . to_s
24+ end
25+ end
26+
27+ # Raised by {JSON::Schema::Reader} when one of its settings indicate
28+ # a schema should not be read.
29+ class ReadRefused < ReadError
30+ private
31+ def error_message
32+ "Read of #{ type_string } at #{ location } refused"
33+ end
34+ end
35+
36+ # Raised by {JSON::Schema::Reader} when an attempt to read a schema fails
37+ class ReadFailed < ReadError
38+ private
39+ def error_message
40+ "Read of #{ type_string } at #{ location } failed"
1941 end
2042 end
2143
@@ -58,6 +80,8 @@ def initialize(options = {})
5880 # @raise [JSON::Schema::ReadRefused] if +accept_uri+ or +accept_file+
5981 # indicated the schema could not be read
6082 # @raise [JSON::Schema::ParseError] if the schema was not a valid JSON object
83+ # @raise [JSON::Schema::ReadFailed] if reading the location was acceptable but the
84+ # attempt to retrieve it failed
6185 def read ( location )
6286 uri = JSON ::Util ::URI . parse ( location . to_s )
6387 body = if uri . scheme . nil? || uri . scheme == 'file'
@@ -98,6 +122,8 @@ def read_uri(uri)
98122 else
99123 raise JSON ::Schema ::ReadRefused . new ( uri . to_s , :uri )
100124 end
125+ rescue OpenURI ::HTTPError , SocketError
126+ raise JSON ::Schema ::ReadFailed . new ( uri . to_s , :uri )
101127 end
102128
103129 def read_file ( pathname )
@@ -106,6 +132,8 @@ def read_file(pathname)
106132 else
107133 raise JSON ::Schema ::ReadRefused . new ( pathname . to_s , :file )
108134 end
135+ rescue Errno ::ENOENT
136+ raise JSON ::Schema ::ReadFailed . new ( pathname . to_s , :file )
109137 end
110138 end
111139 end
0 commit comments