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!(
|
|
|
|
'ActionController::RoutingError' => :not_found,
|
|
|
|
'AbstractController::ActionNotFound' => :not_found,
|
|
|
|
'ActionController::MethodNotAllowed' => :method_not_allowed,
|
2013-04-22 09:09:41 -04:00
|
|
|
'ActionController::UnknownHttpMethod' => :method_not_allowed,
|
2011-12-01 14:02:00 -05:00
|
|
|
'ActionController::NotImplemented' => :not_implemented,
|
2011-07-27 18:37:06 -04:00
|
|
|
'ActionController::UnknownFormat' => :not_acceptable,
|
2012-05-20 05:04:12 -04:00
|
|
|
'ActionController::InvalidAuthenticityToken' => :unprocessable_entity,
|
2013-03-21 14:23:46 -04:00
|
|
|
'ActionDispatch::ParamsParser::ParseError' => :bad_request,
|
2013-01-15 19:07:13 -05:00
|
|
|
'ActionController::BadRequest' => :bad_request,
|
2013-11-02 15:30:03 -04:00
|
|
|
'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)
|
|
|
|
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
|
|
|
|
|
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
|
2012-12-31 14:36:23 -05:00
|
|
|
if application_trace && trace = application_trace.first
|
2012-04-29 19:26:37 -04:00
|
|
|
file, line, _ = trace.split(":")
|
|
|
|
@file = file
|
|
|
|
@line_number = line.to_i
|
|
|
|
source_fragment(@file, @line_number)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-01 14:02:00 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
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
|
|
|
|
backtrace_cleaner.clean(@exception.backtrace, *args)
|
|
|
|
else
|
|
|
|
@exception.backtrace
|
|
|
|
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
|
2011-12-01 14:02:00 -05:00
|
|
|
end
|
2012-05-10 18:50:56 -04:00
|
|
|
end
|