Skip to content

Instantly share code, notes, and snippets.

@look
Created February 24, 2010 23:32
Show Gist options
  • Save look/314024 to your computer and use it in GitHub Desktop.
Save look/314024 to your computer and use it in GitHub Desktop.
#
# Silently fail instead of raising an exception when an error occurs while writing to Solr.
# NOTE: does not fail for reads; you should catch those exceptions, for example in a rescue_from statement.
#
# To configure, add this to an initializer:
# Sunspot.session = SilentFailSessionProxy.new(session_or_proxy)
#
# You can get the existing session with Sunspot.send(:session) (it's a private method in Sunspot 0.18, but not 1.0)
#
# This is for Sunspot 0.18 and would need to be changed a little bit for Sunspot 1.0.
#
class SilentFailSessionProxy
attr_reader :session
delegate :new_search, :search, :configuration, :to => :session
[:index, :index!, :commit, :remove, :remove!, :remove_by_id,
:remove_by_id!, :remove_all, :remove_all!, :dirty?, :commit_if_dirty, :batch].each do |method|
module_eval(<<-RUBY)
def #{method}(*args, &block)
begin
session.#{method}(*args, &block)
rescue => e
Rails.logger.error(e.message)
end
end
RUBY
end
def initialize(session)
@session = session
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment