Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Raise a custom exception for Errno::ECONNREFUSED #51

Open
wants to merge 1 commit into from

4 participants

@jamescook

This helps distinguish which external service is unreachable. Test included.

@mwmitchell
Owner

Hi, thanks for this. Is this just a new exception class? What is the advantage over just using Errno::ECONNREFUSED?

  • Matt
@jamescook

To distinguish between different services that also raise Errno::ECONNREFUSED. I'd like to handle solr errors differently.

@ndushay
Owner

This is clean code; I personally don't have strong feelings on Rsolr custom exception vs. generic. Slight leaning towards custom since it subclasses generic anyway, and it goes along with "invalid response" approach for invalid json and invalid ruby in the current master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 20, 2012
  1. Raise a custom exception for Errno::ECONNREFUSED

    James Cook authored
This page is out of date. Refresh to see the latest.
View
6 lib/rsolr/connection.rb
@@ -15,10 +15,12 @@ def execute client, request_context
response = h.request request
charset = response.type_params["charset"]
{:status => response.code.to_i, :headers => response.to_hash, :body => force_charset(response.body, charset)}
+ rescue Errno::ECONNREFUSED
+ raise RSolr::Error::ConnectionRefused
# catch the undefined closed? exception -- this is a confirmed ruby bug
rescue NoMethodError
$!.message == "undefined method `closed?' for nil:NilClass" ?
- raise(Errno::ECONNREFUSED.new) :
+ raise(RSolr::Error::ConnectionRefused) :
raise($!)
end
end
@@ -67,4 +69,4 @@ def force_charset body, charset
body.force_encoding(charset)
end
-end
+end
View
3  lib/rsolr/error.rb
@@ -37,6 +37,9 @@ def parse_solr_error_response body
end
end
+
+ class ConnectionRefused < ::Errno::ECONNREFUSED
+ end
class Http < RuntimeError
View
14 spec/api/connection_spec.rb
@@ -14,6 +14,20 @@
headers.should == {"content-type"=>"text/xml"}
end
+ context "when the connection is refused" do
+ subject { RSolr::Connection.new }
+
+ it "raises a custom exception" do
+ http_stub = double("Net:HTTP")
+ http_stub.stub(:request){ raise(Errno::ECONNREFUSED.new) }
+
+ subject.stub(:setup_raw_request){ http_stub }
+ subject.stub(:http){ Net::HTTP.new("localhost", 80) }
+
+ lambda{ subject.execute(nil,{}) }.should raise_error(RSolr::Error::ConnectionRefused)
+ end
+ end
+
context "read timeout configuration" do
let(:client) { mock.as_null_object }
Something went wrong with that request. Please try again.