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
# 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,  :layout unless app.respond_to?(:exceptions_layout)
  app.set :redmine, {} unless app.respond_to?(:redmine)
  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 { |k,v| body += "\n#{k.inspect} => #{v.inspect}" }
    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
  end
  app.error 404 do
    response.status = 404
    content_type 'text/html', :charset => "utf-8"
    render settings.exceptions_page, :layout => settings.exceptions_layout
  end
end