class ShowExceptions

Attributes

app[R]

Public Class Methods

new(app) click to toggle source
# File lib/db/show_exceptions.rb, line 6
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/db/show_exceptions.rb, line 10
def call(env)
  app.call(env)
rescue Exception => e
  render_exception(e)
end

Private Instance Methods

error_source_file(e) click to toggle source
# File lib/db/show_exceptions.rb, line 27
def error_source_file(e)
  stack_trace_top(e)[0]
end
extract_formatted_source(e) click to toggle source
# File lib/db/show_exceptions.rb, line 35
def extract_formatted_source(e)
  source_file_name = error_source_file(e)
  source_line_num = source_line_num(e)
  source_lines = extract_source(source_file_name)
  format_source(source_lines, source_line_num)
end
extract_source(file) click to toggle source
# File lib/db/show_exceptions.rb, line 51
def extract_source(file)
  source_file = File.open(file, 'r')
  source_file.readlines
end
format_source(source_lines, source_line_num) click to toggle source
# File lib/db/show_exceptions.rb, line 56
def format_source(source_lines, source_line_num)
  start = [0, source_line_num - 3].max
  lines = source_lines[start..(start + 5)]
  Hash[*(start+1..(lines.count + start)).zip(lines).flatten]
end
formatted_source(file, source_line_num) click to toggle source
# File lib/db/show_exceptions.rb, line 46
def formatted_source(file, source_line_num)
  source_lines = extract_source(file)
  format_source(source_lines, source_line_num)
end
render_exception(e) click to toggle source
# File lib/db/show_exceptions.rb, line 18
def render_exception(e)
  dir_path = File.dirname(__FILE__)
  template_fname = File.join(dir_path, "templates", "error_page.html.erb")
  template = File.read(template_fname)
  body = ERB.new(template).result(binding)

  ['500', {'Content-type' => 'text/html'}, [body]]
end
source_line_num(e) click to toggle source
# File lib/db/show_exceptions.rb, line 42
def source_line_num(e)
  stack_trace_top(e)[1].to_i
end
stack_trace_top(e) click to toggle source
# File lib/db/show_exceptions.rb, line 31
def stack_trace_top(e)
  e.backtrace[0].split(':')
end