2011-12-01 14:46:18 -05:00
|
|
|
require 'action_dispatch/http/request'
|
|
|
|
require 'action_dispatch/middleware/exception_wrapper'
|
2012-07-01 23:00:10 -04:00
|
|
|
require 'action_dispatch/routing/inspector'
|
2014-12-02 12:05:43 -05:00
|
|
|
require 'action_view'
|
|
|
|
require 'action_view/base'
|
2012-06-09 15:54:49 -04:00
|
|
|
|
2014-12-01 17:45:33 -05:00
|
|
|
require 'pp'
|
|
|
|
|
2011-12-01 14:46:18 -05:00
|
|
|
module ActionDispatch
|
|
|
|
# This middleware is responsible for logging exceptions and
|
|
|
|
# showing a debugging page in case the request is local.
|
|
|
|
class DebugExceptions
|
2012-12-11 18:48:05 -05:00
|
|
|
RESCUES_TEMPLATE_PATH = File.expand_path('../templates', __FILE__)
|
2011-12-01 14:46:18 -05:00
|
|
|
|
2014-12-01 17:45:33 -05:00
|
|
|
class DebugView < ActionView::Base
|
|
|
|
def debug_params(params)
|
|
|
|
clean_params = params.clone
|
|
|
|
clean_params.delete("action")
|
|
|
|
clean_params.delete("controller")
|
|
|
|
|
|
|
|
if clean_params.empty?
|
|
|
|
'None'
|
|
|
|
else
|
|
|
|
PP.pp(clean_params, "", 200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def debug_headers(headers)
|
|
|
|
if headers.present?
|
|
|
|
headers.inspect.gsub(',', ",\n")
|
|
|
|
else
|
|
|
|
'None'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def debug_hash(object)
|
|
|
|
object.to_hash.sort_by { |k, _| k.to_s }.map { |k, v| "#{k}: #{v.inspect rescue $!.message}" }.join("\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-06 20:33:19 -04:00
|
|
|
def initialize(app, routes_app = nil, api_only = false)
|
2012-07-01 23:00:10 -04:00
|
|
|
@app = app
|
|
|
|
@routes_app = routes_app
|
2015-07-06 20:33:19 -04:00
|
|
|
@api_only = api_only
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2015-08-06 19:12:06 -04:00
|
|
|
request = ActionDispatch::Request.new env
|
2013-01-06 19:47:49 -05:00
|
|
|
_, headers, body = response = @app.call(env)
|
2011-12-01 14:46:18 -05:00
|
|
|
|
2013-01-06 19:47:49 -05:00
|
|
|
if headers['X-Cascade'] == 'pass'
|
|
|
|
body.close if body.respond_to?(:close)
|
|
|
|
raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
|
2013-01-06 19:47:49 -05:00
|
|
|
response
|
|
|
|
rescue Exception => exception
|
2015-08-06 19:12:06 -04:00
|
|
|
raise exception unless request.show_exceptions?
|
2015-08-23 20:18:02 -04:00
|
|
|
render_exception(request, exception)
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-08-23 20:18:02 -04:00
|
|
|
def render_exception(request, exception)
|
|
|
|
backtrace_cleaner = request.get_header('action_dispatch.backtrace_cleaner')
|
2015-08-06 18:59:30 -04:00
|
|
|
wrapper = ExceptionWrapper.new(backtrace_cleaner, exception)
|
2015-08-23 20:18:02 -04:00
|
|
|
log_error(request, wrapper)
|
2011-12-01 14:46:18 -05:00
|
|
|
|
2015-08-23 20:18:02 -04:00
|
|
|
if request.get_header('action_dispatch.show_detailed_exceptions')
|
2015-07-06 20:33:19 -04:00
|
|
|
if @api_only
|
2015-07-07 11:43:49 -04:00
|
|
|
render_for_api_application(request, wrapper)
|
2013-08-21 08:42:04 -04:00
|
|
|
else
|
2015-07-07 11:43:49 -04:00
|
|
|
render_for_non_api_application(request, wrapper)
|
2013-08-21 08:42:04 -04:00
|
|
|
end
|
2011-12-01 14:46:18 -05:00
|
|
|
else
|
|
|
|
raise exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-07 11:43:49 -04:00
|
|
|
def render_for_non_api_application(request, wrapper)
|
|
|
|
template = create_template(request, wrapper)
|
|
|
|
|
|
|
|
file = "rescues/#{wrapper.rescue_template}"
|
|
|
|
|
|
|
|
if request.xhr?
|
|
|
|
body = template.render(template: file, layout: false, formats: [:text])
|
|
|
|
format = "text/plain"
|
|
|
|
else
|
|
|
|
body = template.render(template: file, layout: 'rescues/layout')
|
|
|
|
format = "text/html"
|
|
|
|
end
|
|
|
|
render(wrapper.status_code, body, format)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_for_api_application(request, wrapper)
|
|
|
|
body = {
|
|
|
|
:status => wrapper.status_code,
|
|
|
|
:error => Rack::Utils::HTTP_STATUS_CODES.fetch(wrapper.status_code, Rack::Utils::HTTP_STATUS_CODES[500]),
|
|
|
|
:exception => wrapper.exception.inspect,
|
|
|
|
:traces => wrapper.traces
|
|
|
|
}
|
|
|
|
|
|
|
|
content_type = request.formats.first
|
|
|
|
to_format = "to_#{content_type.to_sym}"
|
|
|
|
|
|
|
|
if content_type && body.respond_to?(to_format)
|
|
|
|
body = body.public_send(to_format)
|
|
|
|
format = content_type
|
|
|
|
else
|
|
|
|
body = body.to_json
|
|
|
|
format = Mime::JSON
|
|
|
|
end
|
|
|
|
|
|
|
|
render(wrapper.status_code, body, format)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_template(request, wrapper)
|
|
|
|
traces = wrapper.traces
|
|
|
|
|
|
|
|
trace_to_show = 'Application Trace'
|
|
|
|
if traces[trace_to_show].empty? && wrapper.rescue_template != 'routing_error'
|
|
|
|
trace_to_show = 'Full Trace'
|
|
|
|
end
|
|
|
|
|
|
|
|
if source_to_show = traces[trace_to_show].first
|
|
|
|
source_to_show_id = source_to_show[:id]
|
|
|
|
end
|
|
|
|
|
|
|
|
DebugView.new([RESCUES_TEMPLATE_PATH],
|
|
|
|
request: request,
|
|
|
|
exception: wrapper.exception,
|
|
|
|
traces: traces,
|
|
|
|
show_source_idx: source_to_show_id,
|
|
|
|
trace_to_show: trace_to_show,
|
|
|
|
routes_inspector: routes_inspector(wrapper.exception),
|
|
|
|
source_extracts: wrapper.source_extracts,
|
|
|
|
line_number: wrapper.line_number,
|
|
|
|
file: wrapper.file
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-08-21 08:42:04 -04:00
|
|
|
def render(status, body, format)
|
|
|
|
[status, {'Content-Type' => "#{format}; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
|
2015-08-23 20:18:02 -04:00
|
|
|
def log_error(request, wrapper)
|
|
|
|
logger = logger(request)
|
2011-12-01 14:46:18 -05:00
|
|
|
return unless logger
|
|
|
|
|
|
|
|
exception = wrapper.exception
|
|
|
|
|
2011-12-13 14:32:39 -05:00
|
|
|
trace = wrapper.application_trace
|
|
|
|
trace = wrapper.framework_trace if trace.empty?
|
|
|
|
|
2011-12-01 14:46:18 -05:00
|
|
|
ActiveSupport::Deprecation.silence do
|
|
|
|
message = "\n#{exception.class} (#{exception.message}):\n"
|
|
|
|
message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
|
2011-12-13 14:32:39 -05:00
|
|
|
message << " " << trace.join("\n ")
|
2011-12-01 14:46:18 -05:00
|
|
|
logger.fatal("#{message}\n\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-23 20:18:02 -04:00
|
|
|
def logger(request)
|
|
|
|
request.logger || stderr_logger
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def stderr_logger
|
2011-12-21 13:49:25 -05:00
|
|
|
@stderr_logger ||= ActiveSupport::Logger.new($stderr)
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
2012-06-09 15:54:49 -04:00
|
|
|
|
2013-01-05 07:59:00 -05:00
|
|
|
def routes_inspector(exception)
|
2013-01-06 19:54:46 -05:00
|
|
|
if @routes_app.respond_to?(:routes) && (exception.is_a?(ActionController::RoutingError) || exception.is_a?(ActionView::Template::Error))
|
2013-01-05 07:59:00 -05:00
|
|
|
ActionDispatch::Routing::RoutesInspector.new(@routes_app.routes.routes)
|
|
|
|
end
|
|
|
|
end
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
end
|