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

git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@495223 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2007-01-11 13:54:22 +00:00
parent b3d61ea8de
commit 0cad261fb8
5 changed files with 37 additions and 1 deletions

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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