From f81852f14bc461806ec82b9c83a73273098fc924 Mon Sep 17 00:00:00 2001 From: Jason Garber Date: Mon, 25 Jun 2018 20:48:07 -0400 Subject: [PATCH] Add error class and rescue/raise common errors --- README.md | 16 ++++++++-------- lib/webmention/endpoint.rb | 1 + lib/webmention/endpoint/discover.rb | 10 ++++++++++ lib/webmention/endpoint/error.rb | 15 +++++++++++++++ lib/webmention/endpoint/version.rb | 2 +- spec/lib/webmention/endpoint_spec.rb | 8 +++++++- 6 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 lib/webmention/endpoint/error.rb diff --git a/README.md b/README.md index 36a61be..4b8366e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/webmention/endpoint.rb b/lib/webmention/endpoint.rb index c9781f4..a5679f2 100644 --- a/lib/webmention/endpoint.rb +++ b/lib/webmention/endpoint.rb @@ -3,6 +3,7 @@ require 'nokogiri' require 'webmention/endpoint/version' +require 'webmention/endpoint/error' require 'webmention/endpoint/discover' module Webmention diff --git a/lib/webmention/endpoint/discover.rb b/lib/webmention/endpoint/discover.rb index 4b3893d..4491bb6 100644 --- a/lib/webmention/endpoint/discover.rb +++ b/lib/webmention/endpoint/discover.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/webmention/endpoint/error.rb b/lib/webmention/endpoint/error.rb new file mode 100644 index 0000000..19636af --- /dev/null +++ b/lib/webmention/endpoint/error.rb @@ -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 diff --git a/lib/webmention/endpoint/version.rb b/lib/webmention/endpoint/version.rb index 31198ef..9a92b08 100644 --- a/lib/webmention/endpoint/version.rb +++ b/lib/webmention/endpoint/version.rb @@ -1,5 +1,5 @@ module Webmention module Endpoint - VERSION = '0.1.0'.freeze + VERSION = '0.2.0'.freeze end end diff --git a/spec/lib/webmention/endpoint_spec.rb b/spec/lib/webmention/endpoint_spec.rb index 76c2eb9..fa84010 100644 --- a/spec/lib/webmention/endpoint_spec.rb +++ b/spec/lib/webmention/endpoint_spec.rb @@ -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