Module: Ketchup::Exception::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/rails/controller.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#ketchup(&block) ⇒ Object

INTERNAL - The method which is called by #around_filter

Finds the error configuration:

:error    - Class of the Error
:with     - Proc or method name to call. If this is ommited be sure to 
            implement an respond_with_error(err) method.
:remember - Boolean. If true error will be saved in database
            Default true
:notify   - Boolean.
            Default true
            It sends an email to any reciepient which is defined during
            Ketchup configuration. If deliver_mail in Ketchup configuration is
            set to false, sending mail is ignored
:log      - Boolean.
            Default true
            Call the proc that is defined in log_error


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rails/controller.rb', line 75

def ketchup(&block)
  yield
rescue => err
  if Ketchup::Exception.environment.include?(Rails.env.to_sym)
    # Default error configuration
    default_conf = { :with => :respond_with_error, :remember => true, :notify => true, :log => true }
    # Find specific error configuration
    conf = self.class.rescue_errors.find { |error_conf| error_conf[:error] == err.class }
    conf = default_conf.merge(conf || {})
    # save to database
    if Ketchup::Exception.persist
      Ketchup::Handler.action(:database, :exception => err) if conf[:remember]
    end
    # send an email
    if Ketchup::Exception.deliver_mail
      Ketchup::Handler.action(:mail, {:exception => err, :host => request.host_with_port}) if conf[:notify]
    end
    # log error
    if conf[:log]
      Ketchup::Exception.log_error.call(err)
    end 
    # Run error handler
    if conf[:with].is_a? Proc
      conf[:with].call(err)
    else
      self.send(conf[:with], err)
    end
  else
    raise err
  end
end