class ActionDispatch::ExceptionWrapper

Attributes

env[R]
exception[R]
file[R]
line_number[R]

Public Class Methods

new(env, exception) click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 33
def initialize(env, exception)
  @env = env
  @exception = original_exception(exception)

  expand_backtrace if exception.is_a?(SyntaxError) || exception.try(:original_exception).try(:is_a?, SyntaxError)
end
status_code_for_exception(class_name) click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 84
def self.status_code_for_exception(class_name)
  Rack::Utils.status_code(@@rescue_responses[class_name])
end

Public Instance Methods

application_trace() click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 48
def application_trace
  clean_backtrace(:silent)
end
framework_trace() click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 52
def framework_trace
  clean_backtrace(:noise)
end
full_trace() click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 56
def full_trace
  clean_backtrace(:all)
end
rescue_template() click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 40
def rescue_template
  @@rescue_templates[@exception.class.name]
end
source_extracts() click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 88
def source_extracts
  backtrace.map do |trace|
    file, line = trace.split(":")
    line_number = line.to_i

    {
      code: source_fragment(file, line_number),
      line_number: line_number
    }
  end
end
status_code() click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 44
def status_code
  self.class.status_code_for_exception(@exception.class.name)
end
traces() click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 60
def traces
  appplication_trace_with_ids = []
  framework_trace_with_ids = []
  full_trace_with_ids = []

  full_trace.each_with_index do |trace, idx|
    trace_with_id = { id: idx, trace: trace }

    if application_trace.include?(trace)
      appplication_trace_with_ids << trace_with_id
    else
      framework_trace_with_ids << trace_with_id
    end

    full_trace_with_ids << trace_with_id
  end

  {
    "Application Trace" => appplication_trace_with_ids,
    "Framework Trace" => framework_trace_with_ids,
    "Full Trace" => full_trace_with_ids
  }
end

Private Instance Methods

backtrace() click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 102
def backtrace
  Array(@exception.backtrace)
end
backtrace_cleaner() click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 126
def backtrace_cleaner
  @backtrace_cleaner ||= @env['action_dispatch.backtrace_cleaner']
end
clean_backtrace(*args) click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 118
def clean_backtrace(*args)
  if backtrace_cleaner
    backtrace_cleaner.clean(backtrace, *args)
  else
    backtrace
  end
end
expand_backtrace() click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 142
def expand_backtrace
  @exception.backtrace.unshift(
    @exception.to_s.split("\n")
  ).flatten!
end
original_exception(exception) click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 106
def original_exception(exception)
  if registered_original_exception?(exception)
    exception.original_exception
  else
    exception
  end
end
registered_original_exception?(exception) click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 114
def registered_original_exception?(exception)
  exception.respond_to?(:original_exception) && @@rescue_responses.has_key?(exception.original_exception.class.name)
end
source_fragment(path, line) click to toggle source
# File lib/action_dispatch/middleware/exception_wrapper.rb, line 130
def source_fragment(path, line)
  return unless Rails.respond_to?(:root) && Rails.root
  full_path = Rails.root.join(path)
  if File.exist?(full_path)
    File.open(full_path, "r") do |file|
      start = [line - 3, 0].max
      lines = file.each_line.drop(start).take(6)
      Hash[*(start+1..(lines.count+start)).zip(lines).flatten]
    end
  end
end