Skip to content

Commit

Permalink
Raise error when Sphinx returns error
Browse files Browse the repository at this point in the history
  • Loading branch information
mtodd committed Nov 23, 2010
1 parent 79c8c29 commit d298df2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/thinking_sphinx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def initialize(ids)
end
end

# A SphinxError occurs when Sphinx responds with an error due to problematic
# queries or indexes.
class SphinxError < RuntimeError
attr_accessor :results
def initialize(message = nil, results = nil)
super(message)
self.results = results
end
end

# The current version of Thinking Sphinx.
#
# @return [String] The version number as a string
Expand Down
7 changes: 7 additions & 0 deletions lib/thinking_sphinx/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,13 @@ def populate
}
log "Found #{@results[:total_found]} results", :debug,
"Sphinx (#{sprintf("%f", runtime)}s)"

log "Sphinx Daemon returned warning: #{warning}", :error if warning?

if error?
log "Sphinx Daemon returned error: #{error}", :error
raise ThinkingSphinx::SphinxError.new(error, @results)
end
rescue Errno::ECONNREFUSED => err
raise ThinkingSphinx::ConnectionError,
'Connection to Sphinx Daemon (searchd) failed.'
Expand Down
16 changes: 16 additions & 0 deletions spec/thinking_sphinx/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,22 @@
end
end
end

context 'Sphinx errors' do
describe '#error?' do
before :each do
@client.stub! :query => {
:error => @warning = "Not good"
}
# @search.should_receive(:error).and_return(nil)
end
it "should raise an error" do
lambda{
ThinkingSphinx::Search.new.first
}.should raise_error(ThinkingSphinx::SphinxError)
end
end
end
end

describe '#current_page' do
Expand Down

0 comments on commit d298df2

Please sign in to comment.