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'
|
2012-06-09 15:54:49 -04:00
|
|
|
|
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
|
|
|
|
2012-07-01 23:00:10 -04:00
|
|
|
def initialize(app, routes_app = nil)
|
|
|
|
@app = app
|
|
|
|
@routes_app = routes_app
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
begin
|
2012-12-26 08:17:11 -05:00
|
|
|
response = (_, headers, body = @app.call(env))
|
2011-12-01 14:46:18 -05:00
|
|
|
|
2012-12-26 08:17:11 -05:00
|
|
|
if headers['X-Cascade'] == 'pass'
|
2011-12-14 11:03:35 -05:00
|
|
|
body.close if body.respond_to?(:close)
|
2011-12-01 14:46:18 -05:00
|
|
|
raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"
|
|
|
|
end
|
|
|
|
rescue Exception => exception
|
2012-12-26 10:15:18 -05:00
|
|
|
raise exception if env['action_dispatch.show_exceptions'] == false
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
|
2011-12-03 05:38:25 -05:00
|
|
|
exception ? render_exception(env, exception) : response
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def render_exception(env, exception)
|
|
|
|
wrapper = ExceptionWrapper.new(env, exception)
|
|
|
|
log_error(env, wrapper)
|
|
|
|
|
|
|
|
if env['action_dispatch.show_detailed_exceptions']
|
|
|
|
template = ActionView::Base.new([RESCUES_TEMPLATE_PATH],
|
|
|
|
:request => Request.new(env),
|
|
|
|
:exception => wrapper.exception,
|
|
|
|
:application_trace => wrapper.application_trace,
|
|
|
|
:framework_trace => wrapper.framework_trace,
|
2012-06-09 15:54:49 -04:00
|
|
|
:full_trace => wrapper.full_trace,
|
2012-04-29 19:26:37 -04:00
|
|
|
:routes => formatted_routes(exception),
|
|
|
|
:source_extract => wrapper.source_extract,
|
|
|
|
:line_number => wrapper.line_number,
|
|
|
|
:file => wrapper.file
|
2011-12-01 14:46:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
file = "rescues/#{wrapper.rescue_template}"
|
|
|
|
body = template.render(:template => file, :layout => 'rescues/layout')
|
|
|
|
render(wrapper.status_code, body)
|
|
|
|
else
|
|
|
|
raise exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render(status, body)
|
|
|
|
[status, {'Content-Type' => "text/html; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_error(env, wrapper)
|
|
|
|
logger = logger(env)
|
|
|
|
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
|
|
|
|
|
|
|
|
def logger(env)
|
|
|
|
env['action_dispatch.logger'] || stderr_logger
|
|
|
|
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
|
|
|
|
|
|
|
def formatted_routes(exception)
|
2012-07-01 23:00:10 -04:00
|
|
|
return false unless @routes_app.respond_to?(:routes)
|
2012-06-09 15:54:49 -04:00
|
|
|
if exception.is_a?(ActionController::RoutingError) || exception.is_a?(ActionView::Template::Error)
|
2012-07-08 12:25:45 -04:00
|
|
|
inspector = ActionDispatch::Routing::RoutesInspector.new
|
2012-12-17 15:26:46 -05:00
|
|
|
inspector.collect_routes(@routes_app.routes.routes)
|
2012-06-09 15:54:49 -04:00
|
|
|
end
|
|
|
|
end
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
end
|