Module: Padrino::Contrib::ExceptionNotifier

Defined in:
lib/padrino-contrib/exception_notifier.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/padrino-contrib/exception_notifier.rb', line 22

def self.registered(app)
  app.set :exceptions_subject, "Exception" unless app.respond_to?(:exceptions_subject)
  app.set :exceptions_to,      "[email protected]" unless app.respond_to?(:exceptions_to)
  app.set :exceptions_from,    "[email protected]" unless app.respond_to?(:exceptions_from)
  app.set :exceptions_layout,  :application unless app.respond_to?(:exceptions_layout)
  app.set :exceptions_views,   app.views unless app.respond_to?(:exceptions_views)
  app.set :redmine, {} unless app.respond_to?(:redmine)
  app.set :exceptions_params_filter, ['password', 'password_confirmation'] unless app.respond_to?(:exceptions_params_filter)
  app.error 500 do
    boom  = env['sinatra.error']
    body  = ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n  ")
    body += "\n\n---Env:\n"
    env.each { |k,v| body += "\n#{k}: #{v}" }
    body += "\n\n---Params:\n"
    params.each do |k,v|
      if settings.exceptions_params_filter.include?(k)
        body += "\n#{k.inspect} => [FILTERED]"
      else
        body += "\n#{k.inspect} => #{v.inspect}"
      end
    end
    logger.error body
    settings.redmine.each { |k,v| body += "\n#{k.to_s.capitalize}: #{v}" }
    app.email do
      subject "[#{app.exceptions_subject}] #{boom.class} - #{boom.message}"
      to app.exceptions_to
      from app.exceptions_from
      body body
    end
    response.status = 500
    content_type 'text/html', :charset => "utf-8"
    render settings.exceptions_page, :layout => settings.exceptions_layout, :views => settings.exceptions_views
  end
  app.not_found do
    response.status = 404
    content_type 'text/html', :charset => "utf-8"
    render settings.exceptions_page, :layout => settings.exceptions_layout, :views => settings.exceptions_views
  end
end