class ShowExceptions

Attributes

app[R]

Public Class Methods

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

Public Instance Methods

call(env) click to toggle source
# File lib/railz_lite/controllers/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/railz_lite/controllers/show_exceptions.rb, line 27
def error_source_file(e)
  stack_trace_top(e).first
end
extract_formatted_source(e) click to toggle source
# File lib/railz_lite/controllers/show_exceptions.rb, line 39
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/railz_lite/controllers/show_exceptions.rb, line 46
def extract_source(file)
  File.open(file, 'r').readlines
end
format_source(source_lines, source_line_num) click to toggle source
# File lib/railz_lite/controllers/show_exceptions.rb, line 50
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
render_exception(e) click to toggle source
# File lib/railz_lite/controllers/show_exceptions.rb, line 18
def render_exception(e)
  dir_path = File.dirname(__FILE__)
  template_fname = File.join(dir_path, 'templates', 'rescue.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/railz_lite/controllers/show_exceptions.rb, line 35
def source_line_num(e)
  stack_trace_top(e)[1].to_i
end
stack_trace_top(e) click to toggle source
# File lib/railz_lite/controllers/show_exceptions.rb, line 31
def stack_trace_top(e)
  e.backtrace.first.split(':')
end