Skip to content

Commit 65361ee

Browse files
committed
Simplify exception handling and update documentation
1 parent 44f2d23 commit 65361ee

5 files changed

Lines changed: 46 additions & 54 deletions

File tree

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,15 @@ client.endpoints
7070

7171
### Exception Handling
7272

73-
There are several exceptions that may be raised by indieweb-endpoints-ruby's underlying dependencies. These errors are raised as subclasses of `IndieWebEndpointsError` (which itself is a subclass of `StandardError`).
73+
There are several exceptions that may be raised by indieweb-endpoints-ruby's underlying dependencies. These errors are raised as subclasses of `IndieWeb::Endpoints::Error` (which itself is a subclass of `StandardError`).
7474

7575
From [sporkmonger/addressable](https://github.com/sporkmonger/addressable):
7676

7777
- `IndieWeb::Endpoints::InvalidURIError`
7878

7979
From [httprb/http](https://github.com/httprb/http):
8080

81-
- `IndieWeb::Endpoints::ConnectionError`
82-
- `IndieWeb::Endpoints::TimeoutError`
83-
- `IndieWeb::Endpoints::TooManyRedirectsError`
81+
- `IndieWeb::Endpoints::HttpError`
8482

8583
## Contributing
8684

lib/indieweb/endpoints/client.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ class Client
1212
#
1313
# @param url [String] an absolute URL
1414
def initialize(url)
15-
raise ArgumentError, "url must be a String (given #{url.class})" unless url.is_a?(String)
16-
17-
@url = url
18-
19-
raise ArgumentError, "url (#{url}) must be an absolute URL (e.g. https://example.com)" unless uri.absolute? && uri.scheme.match?(/^https?$/)
15+
@url = url.to_str
2016
end
2117

2218
# @return [String]
@@ -34,10 +30,8 @@ def endpoints
3430
# @return [HTTP::Response]
3531
def response
3632
@response ||= HTTP.follow(max_hops: 20).headers(HTTP_HEADERS_OPTS).timeout(connect: 5, read: 5).get(uri)
37-
rescue HTTP::ConnectionError,
38-
HTTP::TimeoutError,
39-
HTTP::Redirector::TooManyRedirectsError => exception
40-
raise IndieWeb::Endpoints.const_get(exception.class.name.split('::').last), exception
33+
rescue HTTP::Error => exception
34+
raise HttpError, exception
4135
end
4236

4337
private
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
module IndieWeb
22
module Endpoints
3-
class IndieWebEndpointsError < StandardError; end
3+
class Error < StandardError; end
44

5-
class ArgumentError < IndieWebEndpointsError; end
5+
class ArgumentError < Error; end
66

7-
class ConnectionError < IndieWebEndpointsError; end
7+
class HttpError < Error; end
88

9-
class InvalidURIError < IndieWebEndpointsError; end
10-
11-
class TimeoutError < IndieWebEndpointsError; end
12-
13-
class TooManyRedirectsError < IndieWebEndpointsError; end
9+
class InvalidURIError < Error; end
1410
end
1511
end

spec/lib/indieweb/endpoints/client_response_spec.rb

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
request.to_raise(HTTP::ConnectionError)
1010
end
1111

12-
it 'raises a ConnectionError' do
13-
expect { client.response }.to raise_error(IndieWeb::Endpoints::ConnectionError)
12+
it 'raises an HttpError' do
13+
expect { client.response }.to raise_error(IndieWeb::Endpoints::HttpError)
1414
end
1515
end
1616

@@ -19,8 +19,8 @@
1919
request.to_raise(HTTP::TimeoutError)
2020
end
2121

22-
it 'raises a TimeoutError' do
23-
expect { client.response }.to raise_error(IndieWeb::Endpoints::TimeoutError)
22+
it 'raises an HttpError' do
23+
expect { client.response }.to raise_error(IndieWeb::Endpoints::HttpError)
2424
end
2525
end
2626

@@ -29,8 +29,36 @@
2929
request.to_raise(HTTP::Redirector::TooManyRedirectsError)
3030
end
3131

32-
it 'raises a TooManyRedirectsError' do
33-
expect { client.response }.to raise_error(IndieWeb::Endpoints::TooManyRedirectsError)
32+
it 'raises an HttpError' do
33+
expect { client.response }.to raise_error(IndieWeb::Endpoints::HttpError)
34+
end
35+
end
36+
37+
context 'when given an invalid URL' do
38+
let(:url) { 'http:' }
39+
40+
it 'raises an InvalidURIError' do
41+
expect { client.response }.to raise_error(IndieWeb::Endpoints::InvalidURIError)
42+
end
43+
end
44+
45+
context 'when given a relative URL' do
46+
let(:url) { '../foo/bar/biz/baz' }
47+
48+
it 'raises an HttpError' do
49+
message = 'unknown scheme: '
50+
51+
expect { client.response }.to raise_error(IndieWeb::Endpoints::HttpError, message)
52+
end
53+
end
54+
55+
context 'when given a URL with an invalid protocol' do
56+
let(:url) { 'file:///foo/bar/baz' }
57+
58+
it 'raises an HttpError' do
59+
message = 'unknown scheme: file'
60+
61+
expect { client.response }.to raise_error(IndieWeb::Endpoints::HttpError, message)
3462
end
3563
end
3664
end
Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,7 @@
11
RSpec.describe IndieWeb::Endpoints::Client do
2-
context 'when not given a String' do
3-
it 'raises an ArgumentError' do
4-
message = 'url must be a String (given NilClass)'
5-
6-
expect { described_class.new(nil) }.to raise_error(IndieWeb::Endpoints::ArgumentError, message)
7-
end
8-
end
9-
10-
context 'when given an invalid URL' do
11-
it 'raises an InvalidURIError' do
12-
expect { described_class.new('http:') }.to raise_error(IndieWeb::Endpoints::InvalidURIError)
13-
end
14-
end
15-
16-
context 'when given a relative URL' do
17-
it 'raises an ArgumentError' do
18-
message = 'url (../foo/bar/biz/baz) must be an absolute URL (e.g. https://example.com)'
19-
20-
expect { described_class.new('../foo/bar/biz/baz') }.to raise_error(IndieWeb::Endpoints::ArgumentError, message)
21-
end
22-
end
23-
24-
context 'when given a URL with an invalid protocol' do
25-
it 'raises an ArgumentError' do
26-
message = 'url (file:///foo/bar/baz) must be an absolute URL (e.g. https://example.com)'
27-
28-
expect { described_class.new('file:///foo/bar/baz') }.to raise_error(IndieWeb::Endpoints::ArgumentError, message)
2+
context 'when not given a String-like object' do
3+
it 'raises a NoMethodError' do
4+
expect { described_class.new(nil) }.to raise_error(NoMethodError)
295
end
306
end
317
end

0 commit comments

Comments
 (0)