2011-12-01 14:46:18 -05:00
|
|
|
require 'action_controller/metal/exceptions'
|
2013-12-02 16:36:58 -05:00
|
|
|
require 'active_support/core_ext/module/attribute_accessors'
|
2011-12-01 14:02:00 -05:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
class ExceptionWrapper
|
|
|
|
cattr_accessor :rescue_responses
|
|
|
|
@@rescue_responses = Hash.new(:internal_server_error)
|
|
|
|
@@rescue_responses.merge!(
|
2014-10-14 11:51:23 -04:00
|
|
|
'ActionController::RoutingError' => :not_found,
|
|
|
|
'AbstractController::ActionNotFound' => :not_found,
|
|
|
|
'ActionController::MethodNotAllowed' => :method_not_allowed,
|
|
|
|
'ActionController::UnknownHttpMethod' => :method_not_allowed,
|
|
|
|
'ActionController::NotImplemented' => :not_implemented,
|
|
|
|
'ActionController::UnknownFormat' => :not_acceptable,
|
|
|
|
'ActionController::InvalidAuthenticityToken' => :unprocessable_entity,
|
|
|
|
'ActionController::InvalidCrossOriginRequest' => :unprocessable_entity,
|
|
|
|
'ActionDispatch::ParamsParser::ParseError' => :bad_request,
|
|
|
|
'ActionController::BadRequest' => :bad_request,
|
|
|
|
'ActionController::ParameterMissing' => :bad_request
|
2011-12-01 14:02:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
cattr_accessor :rescue_templates
|
|
|
|
@@rescue_templates = Hash.new('diagnostics')
|
|
|
|
@@rescue_templates.merge!(
|
|
|
|
'ActionView::MissingTemplate' => 'missing_template',
|
|
|
|
'ActionController::RoutingError' => 'routing_error',
|
|
|
|
'AbstractController::ActionNotFound' => 'unknown_action',
|
|
|
|
'ActionView::Template::Error' => 'template_error'
|
|
|
|
)
|
|
|
|
|
2012-04-29 19:26:37 -04:00
|
|
|
attr_reader :env, :exception, :line_number, :file
|
2011-12-01 14:02:00 -05:00
|
|
|
|
|
|
|
def initialize(env, exception)
|
|
|
|
@env = env
|
|
|
|
@exception = original_exception(exception)
|
2014-01-30 09:33:49 -05:00
|
|
|
|
|
|
|
expand_backtrace if exception.is_a?(SyntaxError) || exception.try(:original_exception).try(:is_a?, SyntaxError)
|
2011-12-01 14:02:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def rescue_template
|
|
|
|
@@rescue_templates[@exception.class.name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_code
|
2012-09-16 15:48:24 -04:00
|
|
|
self.class.status_code_for_exception(@exception.class.name)
|
2011-12-01 14:02:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def application_trace
|
|
|
|
clean_backtrace(:silent)
|
|
|
|
end
|
|
|
|
|
|
|
|
def framework_trace
|
|
|
|
clean_backtrace(:noise)
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_trace
|
|
|
|
clean_backtrace(:all)
|
|
|
|
end
|
|
|
|
|
2014-11-01 17:14:42 -04:00
|
|
|
def traces
|
|
|
|
appplication_trace_with_ids = []
|
|
|
|
framework_trace_with_ids = []
|
|
|
|
full_trace_with_ids = []
|
|
|
|
|
2014-11-16 10:17:06 -05:00
|
|
|
full_trace.each_with_index do |trace, idx|
|
|
|
|
trace_with_id = { id: idx, trace: trace }
|
2014-11-01 17:14:42 -04:00
|
|
|
|
2014-11-16 10:17:06 -05:00
|
|
|
appplication_trace_with_ids << trace_with_id if application_trace.include?(trace)
|
|
|
|
framework_trace_with_ids << trace_with_id if framework_trace.include?(trace)
|
|
|
|
full_trace_with_ids << trace_with_id
|
2014-11-01 17:14:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
"Application Trace" => appplication_trace_with_ids,
|
|
|
|
"Framework Trace" => framework_trace_with_ids,
|
|
|
|
"Full Trace" => full_trace_with_ids
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-09-16 15:48:24 -04:00
|
|
|
def self.status_code_for_exception(class_name)
|
|
|
|
Rack::Utils.status_code(@@rescue_responses[class_name])
|
|
|
|
end
|
|
|
|
|
2012-04-29 19:26:37 -04:00
|
|
|
def source_extract
|
2014-11-16 10:17:06 -05:00
|
|
|
backtrace.map do |trace|
|
2014-07-30 04:35:47 -04:00
|
|
|
file, line = trace.split(":")
|
|
|
|
line_number = line.to_i
|
|
|
|
{
|
|
|
|
code: source_fragment(file, line_number),
|
|
|
|
file: file,
|
|
|
|
line_number: line_number
|
|
|
|
}
|
2014-11-16 10:17:06 -05:00
|
|
|
end
|
2012-04-29 19:26:37 -04:00
|
|
|
end
|
|
|
|
|
2011-12-01 14:02:00 -05:00
|
|
|
private
|
|
|
|
|
2014-11-16 10:17:06 -05:00
|
|
|
def backtrace
|
|
|
|
Array(@exception.backtrace)
|
|
|
|
end
|
|
|
|
|
2011-12-01 14:02:00 -05:00
|
|
|
def original_exception(exception)
|
|
|
|
if registered_original_exception?(exception)
|
|
|
|
exception.original_exception
|
|
|
|
else
|
|
|
|
exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def registered_original_exception?(exception)
|
|
|
|
exception.respond_to?(:original_exception) && @@rescue_responses.has_key?(exception.original_exception.class.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_backtrace(*args)
|
|
|
|
if backtrace_cleaner
|
2014-11-16 10:17:06 -05:00
|
|
|
backtrace_cleaner.clean(backtrace, *args)
|
2011-12-01 14:02:00 -05:00
|
|
|
else
|
2014-11-16 10:17:06 -05:00
|
|
|
backtrace
|
2011-12-01 14:02:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def backtrace_cleaner
|
|
|
|
@backtrace_cleaner ||= @env['action_dispatch.backtrace_cleaner']
|
|
|
|
end
|
2012-04-29 19:26:37 -04:00
|
|
|
|
|
|
|
def source_fragment(path, line)
|
2012-12-31 14:36:23 -05:00
|
|
|
return unless Rails.respond_to?(:root) && Rails.root
|
2012-04-29 19:26:37 -04:00
|
|
|
full_path = Rails.root.join(path)
|
2013-11-01 04:49:57 -04:00
|
|
|
if File.exist?(full_path)
|
2012-04-29 19:26:37 -04:00
|
|
|
File.open(full_path, "r") do |file|
|
|
|
|
start = [line - 3, 0].max
|
2013-01-04 09:30:09 -05:00
|
|
|
lines = file.each_line.drop(start).take(6)
|
2012-04-29 19:26:37 -04:00
|
|
|
Hash[*(start+1..(lines.count+start)).zip(lines).flatten]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-30 09:33:49 -05:00
|
|
|
|
|
|
|
def expand_backtrace
|
|
|
|
@exception.backtrace.unshift(
|
|
|
|
@exception.to_s.split("\n")
|
2014-07-30 04:35:47 -04:00
|
|
|
).flatten!
|
2014-01-30 09:33:49 -05:00
|
|
|
end
|
2011-12-01 14:02:00 -05:00
|
|
|
end
|
2012-05-10 18:50:56 -04:00
|
|
|
end
|