Skip to content

Commit

Permalink
Wrap inner exceptions when parsing responses within Riddle::ResponseE…
Browse files Browse the repository at this point in the history
…rror.
  • Loading branch information
pat committed May 4, 2013
1 parent eb7db52 commit 2a2c6ca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 7 additions & 1 deletion lib/riddle/client.rb
Expand Up @@ -4,7 +4,9 @@

module Riddle
class VersionError < StandardError; end
class ResponseError < StandardError; end
class ResponseError < StandardError
attr_accessor :original
end
class OutOfBoundsError < StandardError; end

# This class was heavily based on the existing Client API by Dmytro Shteflyuk
Expand Down Expand Up @@ -290,6 +292,10 @@ def run

@queue.clear
results
rescue => original
error = ResponseError.new original.message
error.original = original
raise error
end

# Query the Sphinx daemon - defaulting to all indices, but you can specify
Expand Down
12 changes: 6 additions & 6 deletions spec/functional/connection_spec.rb
Expand Up @@ -8,24 +8,24 @@ class RiddleSpecConnectionProcError < StandardError; end
after :each do
Riddle::Client.connection = nil
end

describe '.connection' do
it "should use the given block" do
Riddle::Client.connection = lambda { |client|
TCPSocket.new(client.server, client.port)
}
client.query('smith').should be_kind_of(Hash)
end

it "should fail with errors from the given block" do
Riddle::Client.connection = lambda { |client|
raise RiddleSpecConnectionProcError
}
lambda { client.query('smith') }.
should raise_error(RiddleSpecConnectionProcError)
should raise_error(Riddle::ResponseError)
end
end

describe '#connection' do
it "use the given block" do
client.connection = lambda { |client|
Expand All @@ -39,7 +39,7 @@ class RiddleSpecConnectionProcError < StandardError; end
raise RiddleSpecConnectionProcError
}
lambda { client.query('smith') }.
should raise_error(RiddleSpecConnectionProcError)
should raise_error(Riddle::ResponseError)
end

it "should prioritise instance over class connection" do
Expand All @@ -49,7 +49,7 @@ class RiddleSpecConnectionProcError < StandardError; end
client.connection = lambda { |client|
TCPSocket.new(client.server, client.port)
}

lambda { client.query('smith') }.should_not raise_error
end
end
Expand Down

0 comments on commit 2a2c6ca

Please sign in to comment.