2016-08-06 12:51:43 -04:00
|
|
|
require "active_support/core_ext/module/attribute_accessors"
|
|
|
|
require "rack/utils"
|
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!(
|
2016-08-06 12:51:43 -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,
|
|
|
|
"Rack::QueryParser::ParameterTypeError" => :bad_request,
|
|
|
|
"Rack::QueryParser::InvalidParameterError" => :bad_request
|
2011-12-01 14:02:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
cattr_accessor :rescue_templates
|
2016-08-06 12:51:43 -04:00
|
|
|
@@rescue_templates = Hash.new("diagnostics")
|
2011-12-01 14:02:00 -05:00
|
|
|
@@rescue_templates.merge!(
|
2016-08-06 12:51:43 -04:00
|
|
|
"ActionView::MissingTemplate" => "missing_template",
|
|
|
|
"ActionController::RoutingError" => "routing_error",
|
|
|
|
"AbstractController::ActionNotFound" => "unknown_action",
|
|
|
|
"ActionView::Template::Error" => "template_error"
|
2011-12-01 14:02:00 -05:00
|
|
|
)
|
|
|
|
|
2015-08-06 18:59:30 -04:00
|
|
|
attr_reader :backtrace_cleaner, :exception, :line_number, :file
|
2011-12-01 14:02:00 -05:00
|
|
|
|
2015-08-06 18:59:30 -04:00
|
|
|
def initialize(backtrace_cleaner, exception)
|
|
|
|
@backtrace_cleaner = backtrace_cleaner
|
2011-12-01 14:02:00 -05:00
|
|
|
@exception = original_exception(exception)
|
2014-01-30 09:33:49 -05:00
|
|
|
|
2015-11-03 09:54:34 -05:00
|
|
|
expand_backtrace if exception.is_a?(SyntaxError) || exception.cause.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
|
2015-09-20 12:32:54 -04:00
|
|
|
application_trace_with_ids = []
|
2014-11-01 17:14:42 -04:00
|
|
|
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-13 17:59:45 -05:00
|
|
|
if application_trace.include?(trace)
|
2015-09-20 12:32:54 -04:00
|
|
|
application_trace_with_ids << trace_with_id
|
2014-11-13 17:59:45 -05:00
|
|
|
else
|
|
|
|
framework_trace_with_ids << trace_with_id
|
|
|
|
end
|
|
|
|
|
2014-11-16 10:17:06 -05:00
|
|
|
full_trace_with_ids << trace_with_id
|
2014-11-01 17:14:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
2015-09-20 12:32:54 -04:00
|
|
|
"Application Trace" => application_trace_with_ids,
|
2014-11-01 17:14:42 -04:00
|
|
|
"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
|
|
|
|
|
2014-11-03 15:59:45 -05:00
|
|
|
def source_extracts
|
2014-11-16 10:17:06 -05:00
|
|
|
backtrace.map do |trace|
|
2015-01-31 13:00:33 -05:00
|
|
|
file, line_number = extract_file_and_line_number(trace)
|
2014-11-03 15:59:45 -05:00
|
|
|
|
2014-07-30 04:35:47 -04:00
|
|
|
{
|
|
|
|
code: source_fragment(file, line_number),
|
|
|
|
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)
|
2015-11-03 09:54:34 -05:00
|
|
|
if @@rescue_responses.has_key?(exception.cause.class.name)
|
|
|
|
exception.cause
|
2011-12-01 14:02:00 -05:00
|
|
|
else
|
|
|
|
exception
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
2015-01-31 13:00:33 -05:00
|
|
|
def extract_file_and_line_number(trace)
|
|
|
|
# Split by the first colon followed by some digits, which works for both
|
|
|
|
# Windows and Unix path styles.
|
|
|
|
file, line = trace.match(/^(.+?):(\d+).*$/, &:captures) || trace
|
|
|
|
[file, line.to_i]
|
|
|
|
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
|