Skip to content

Commit

Permalink
started tagging http client errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiii committed Dec 1, 2012
1 parent 862f3a4 commit d1e29a9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/httpi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class TimeoutError < Error; end
class NotSupportedError < Error; end
class NotImplementedError < Error; end

module ConnectionError; end

class SSLError < Error
def initialize(message = nil, original = $!)
super(message)
Expand Down
3 changes: 3 additions & 0 deletions lib/httpi/adapter/curb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def request(method)
raise SSLError
rescue Curl::Err::SSLPeerCertificateError
raise SSLError
rescue Curl::Err::ConnectionFailedError # connection refused
$!.extend ConnectionError
raise
end

private
Expand Down
3 changes: 3 additions & 0 deletions lib/httpi/adapter/httpclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def request(method)
respond_with @client.request(method, @request.url, nil, @request.body, @request.headers)
rescue OpenSSL::SSL::SSLError
raise SSLError
rescue Errno::ECONNREFUSED # connection refused
$!.extend ConnectionError
raise
end

private
Expand Down
3 changes: 3 additions & 0 deletions lib/httpi/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def request(method)
end
rescue OpenSSL::SSL::SSLError
raise SSLError
rescue Errno::ECONNREFUSED # connection refused
$!.extend ConnectionError
raise
end

private
Expand Down
41 changes: 41 additions & 0 deletions spec/httpi/error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "spec_helper"
require "support/error_helper"

describe HTTPI do
include ErrorHelper

context "with :httpclient" do
it "tags Errno::ECONNREFUSED with HTTPI::ConnectionError" do
expect_error(Errno::ECONNREFUSED, "Connection refused - connect(2)").to be_tagged_with(HTTPI::ConnectionError)
end

def fake_error(error, message)
request(:httpclient) { |client| client.expects(:request).raises(error, message) }
end
end

context "with :curb" do
it "tags Curl::Err::ConnectionFailedError with HTTPI::ConnectionError" do
expect_error(Curl::Err::ConnectionFailedError, "Curl::Err::ConnectionFailedError").to be_tagged_with(HTTPI::ConnectionError)
end

def fake_error(error, message)
request(:curb) { |client| client.expects(:send).raises(error, message) }
end
end

context "with :net_http" do
it "tags Errno::ECONNREFUSED with HTTPI::ConnectionError" do
expect_error(Errno::ECONNREFUSED, "Connection refused - connect(2)").to be_tagged_with(HTTPI::ConnectionError)
end

def fake_error(error, message)
request(:net_http) { |client| client.expects(:start).raises(error, message) }
end
end

def request(adapter)
HTTPI.get("http://example.com", adapter) { |client| yield client }
end

end
26 changes: 26 additions & 0 deletions spec/support/error_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module ErrorHelper

class Expectation

def initialize(error, spec)
@error = error
@spec = spec
end

def to(tag_error)
@spec.expect(@error).to @spec.be_a(tag_error)
end

end

def expect_error(error_to_raise, message)
fake_error(error_to_raise, message)
rescue => error
Expectation.new(error, self)
end

def be_tagged_with(tag_error)
tag_error
end

end

0 comments on commit d1e29a9

Please sign in to comment.