Skip to content
This repository has been archived by the owner on Apr 25, 2019. It is now read-only.

Commit

Permalink
Add error class and rescue/raise common errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber623 committed Jun 26, 2018
1 parent 5070e2b commit f81852f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ puts discoverer.response # returns HTTP::Response

### Exception Handling

There are several exceptions that may be raised by webmention-endpoint-ruby's underlying dependencies:
There are several exceptions that may be raised by webmention-endpoint-ruby's underlying dependencies. These errors are raised as subclasses of `Webmention::Endpoint::Error` (which itself is a subclass of `StandardError`).

- `Addressable::URI::InvalidURIError`
- `HTTP::ConnectionError`
- `HTTP::TimeoutError`
- `HTTP::Redirector::TooManyRedirectsError`
From [sporkmonger/addressable](/~https://github.com/sporkmonger/addressable):

You'll want to make sure your code gracefully handles these exceptions. For more on each of these exceptions, see:
- `Webmention::Endpoint::InvalidURIError`

- [sporkmonger/addressable](/~https://github.com/sporkmonger/addressable) – Source code for the Addressable Ruby gem
- [httprb/http](/~https://github.com/httprb/http) – Source code for the HTTP Ruby gem
From [httprb/http](/~https://github.com/httprb/http):

- `Webmention::Endpoint::ConnectionError`
- `Webmention::Endpoint::TimeoutError`
- `Webmention::Endpoint::TooManyRedirectsError`

## Contributing

Expand Down
1 change: 1 addition & 0 deletions lib/webmention/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'nokogiri'

require 'webmention/endpoint/version'
require 'webmention/endpoint/error'
require 'webmention/endpoint/discover'

module Webmention
Expand Down
10 changes: 10 additions & 0 deletions lib/webmention/endpoint/discover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def initialize(url)

@url = url
@uri = Addressable::URI.parse(url)
rescue Addressable::URI::InvalidURIError => error
raise InvalidURIError, error
end

def endpoint
Expand All @@ -36,6 +38,12 @@ def response
connect: 10,
read: 10
).get(uri)
rescue HTTP::ConnectionError => error
raise ConnectionError, error
rescue HTTP::TimeoutError => error
raise TimeoutError, error
rescue HTTP::Redirector::TooManyRedirectsError => error
raise TooManyRedirectsError, error
end

private
Expand All @@ -49,6 +57,8 @@ def absolutize(base:, relative:)
return relative if relative_uri.absolute?

(base_uri + relative_uri).to_s
rescue Addressable::URI::InvalidURIError => error
raise InvalidURIError, error
end

def endpoint_from_body
Expand Down
15 changes: 15 additions & 0 deletions lib/webmention/endpoint/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Webmention
module Endpoint
class Error < StandardError; end

class ArgumentError < Error; end

class ConnectionError < Error; end

class InvalidURIError < Error; end

class TimeoutError < Error; end

class TooManyRedirectsError < Error; end
end
end
2 changes: 1 addition & 1 deletion lib/webmention/endpoint/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Webmention
module Endpoint
VERSION = '0.1.0'.freeze
VERSION = '0.2.0'.freeze
end
end
8 changes: 7 additions & 1 deletion spec/lib/webmention/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
let(:message) { 'url must be a String (given NilClass)' }

it 'raises an ArgumentError' do
expect { described_class.discover(nil) }.to raise_error(ArgumentError, message)
expect { described_class.discover(nil) }.to raise_error(Webmention::Endpoint::ArgumentError, message)
end
end

context 'when given an invalid URL' do
it 'raises an InvalidURIError' do
expect { described_class.discover('http:') }.to raise_error(Webmention::Endpoint::InvalidURIError)
end
end

Expand Down

0 comments on commit f81852f

Please sign in to comment.