Skip to content

Commit 3f07ef0

Browse files
benfalkiainbeeston
authored andcommitted
Attach URI to Read Error
When getting a `404` or `500` error it was impossible to know which file was causing the problem with a schema that had many external references. This attaches the uri that failed to the error if it's an OpenURI error, which makes is easy to know what refrenced file was the culprit.
1 parent a485620 commit 3f07ef0

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1616
### Changed
1717
- Made all `validate*` methods on `JSON::Validator` ultimately call `validate!`
1818
- Updated addressable dependency to 2.4.0
19+
- Attached failed `uri` or `pathname` to read errors for more meaning
1920

2021
## [2.6.2] - 2016-05-13
2122

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ CONTRIBUTORS
3232
* Ben Kirzhner - @benkirzhner
3333
* RST-J - @RST-J
3434
* Christian Treppo - @treppo
35+
* Benjamin Falk - @benfalk

lib/json-schema/schema/reader.rb

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
module 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

test/bad_schema_ref_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ def test_bad_uri_ref
1919
}
2020

2121
data = [1,2,3]
22-
assert_raises(Errno::ENOENT) do
22+
error = assert_raises(JSON::Schema::ReadFailed) do
2323
JSON::Validator.validate(schema,data)
2424
end
25+
26+
expanded_path = File.expand_path("../../google.json", __FILE__)
27+
28+
assert_equal(:file, error.type)
29+
assert_equal(expanded_path, error.location)
30+
assert_equal("Read of file at #{expanded_path} failed", error.message)
2531
end
2632

2733
def test_bad_host_ref
@@ -32,8 +38,12 @@ def test_bad_host_ref
3238
}
3339

3440
data = [1,2,3]
35-
assert_raises(SocketError, OpenURI::HTTPError) do
41+
error = assert_raises(JSON::Schema::ReadFailed) do
3642
JSON::Validator.validate(schema,data)
3743
end
44+
45+
assert_equal(:uri, error.type)
46+
assert_equal("http://ppcheesecheseunicornnuuuurrrrr.example.invalid/json.schema", error.location)
47+
assert_equal("Read of URI at http://ppcheesecheseunicornnuuuurrrrr.example.invalid/json.schema failed", error.message)
3848
end
3949
end

test/schema_reader_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ def test_accept_file_proc
5757

5858
def test_file_scheme
5959
reader = JSON::Schema::Reader.new(:accept_uri => true, :accept_file => false)
60-
assert_raises(JSON::Schema::ReadRefused) do
60+
error = assert_raises(JSON::Schema::ReadRefused) do
6161
reader.read('file://' + ADDRESS_SCHEMA_PATH)
6262
end
63+
64+
assert_equal(:file, error.type)
65+
assert_equal(ADDRESS_SCHEMA_PATH, error.location)
66+
assert_equal("Read of file at #{ADDRESS_SCHEMA_PATH} refused", error.message)
6367
end
6468

6569
def test_parse_error

0 commit comments

Comments
 (0)