Class: NeverForget::ExceptionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/never_forget/exception_handler.rb

Constant Summary collapse

TEMPLATE_FILE =
File.expand_path('../list_exceptions.erb', __FILE__)

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ExceptionHandler

Returns a new instance of ExceptionHandler.



9
10
11
12
# File 'lib/never_forget/exception_handler.rb', line 9

def initialize(app, options = {})
  @app = app
  @options = {:list_path => '/_exceptions'}.update(options)
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/never_forget/exception_handler.rb', line 23

def call(env)
  if env['PATH_INFO'] == File.join('/', @options[:list_path])
    begin
      body = render_exceptions_view
      [200, {'content-type' => 'text/html'}, [body]]
    rescue
      [500, {'content-type' => 'text/plain'}, ["%s: %s\n\n%s" % [
        $!.class.name,
        $!.message,
        $!.backtrace.join("\n")
      ]]]
    end
  else
    forward(env)
  end
end

#exceptions_templateObject



47
48
49
# File 'lib/never_forget/exception_handler.rb', line 47

def exceptions_template
  File.read(TEMPLATE_FILE)
end

#forward(env) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/never_forget/exception_handler.rb', line 14

def forward(env)
  begin
    @app.call(env)
  rescue StandardError, ScriptError => error
    NeverForget.log(error, env)
    raise error
  end
end

#render_exceptions_viewObject



40
41
42
43
44
45
# File 'lib/never_forget/exception_handler.rb', line 40

def render_exceptions_view
  template = Erubis::Eruby.new(exceptions_template)
  context = Erubis::Context.new(:recent => Exception.recent)
  context.extend TemplateHelpers
  template.evaluate(context)
end