Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-100: added Exception handling to solrb (submitted by William Groppe)

Erik Hatcher0cad261fb8fa
diff --git a/client/ruby/solrb/lib/solr.rb b/client/ruby/solrb/lib/solr.rb
index 8ecd326bc23..f1b4fe459b4 100755
--- a/client/ruby/solrb/lib/solr.rb
+++ b/client/ruby/solrb/lib/solr.rb
@@ -10,6 +10,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+require 'solr/exception'
require 'solr/request'
require 'solr/connection'
require 'solr/response'
\ No newline at end of file
diff --git a/client/ruby/solrb/lib/solr/connection.rb b/client/ruby/solrb/lib/solr/connection.rb
index f16e5d192dd..a4b3c0c70e6 100755
--- a/client/ruby/solrb/lib/solr/connection.rb
+++ b/client/ruby/solrb/lib/solr/connection.rb
@@ -33,7 +33,12 @@ module Solr
http.request(post)
end
- return response.body
+ case response
+ when Net::HTTPSuccess then response.body
+ else
+ response.error!
+ end
+
end
end
end
diff --git a/client/ruby/solrb/lib/solr/exception.rb b/client/ruby/solrb/lib/solr/exception.rb
new file mode 100644
index 00000000000..f55246ffd7f
--- /dev/null
+++ b/client/ruby/solrb/lib/solr/exception.rb
@@ -0,0 +1,17 @@
+module Solr
+
+ class RequestException < Exception
+ attr_reader :code, :message
+
+ def initialize(code, message)
+ @code = code
+ @message = message
+ end
+
+ def to_s
+ @message
+ end
+
+ end
+
+end
\ No newline at end of file
diff --git a/client/ruby/solrb/lib/solr/response.rb b/client/ruby/solrb/lib/solr/response.rb
index ac235a7525f..17220760456 100755
--- a/client/ruby/solrb/lib/solr/response.rb
+++ b/client/ruby/solrb/lib/solr/response.rb
@@ -15,6 +15,12 @@ module Solr
attr_reader :header, :raw_response, :data
def initialize(body)
@raw_response = body
+ if match = /^<result status="(\d+)"/.match(body)
+ unless 0 == match.captures.first.to_i
+ error = REXML::Document.new(body).root
+ raise RequestException.new(error.attributes["status"], error.text)
+ end
+ end
end
end
diff --git a/client/ruby/solrb/test/unit/request_test.rb b/client/ruby/solrb/test/unit/request_test.rb
index ad782a076f3..426f4205e77 100755
--- a/client/ruby/solrb/test/unit/request_test.rb
+++ b/client/ruby/solrb/test/unit/request_test.rb
@@ -34,4 +34,11 @@ class RequestTest < Test::Unit::TestCase
request = Solr::AddDocumentRequest.new({:title => "title"})
assert_equal("<add><doc><field name='title'>title</field></doc></add>", request.to_http_body)
end
+
+ def test_response_error
+ assert_raise(Solr::RequestException) do
+ new Solr::Response.new("<result status=\"400\">ERROR:</result>")
+ end
+ end
+
end