Skip to content

Instantly share code, notes, and snippets.

@outoftime
Forked from look/silent_fail_session_proxy.rb
Created February 25, 2010 00:11
Show Gist options
  • Save outoftime/314067 to your computer and use it in GitHub Desktop.
Save outoftime/314067 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
#
# 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