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)
|
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
|
|
|
|
raise exception if env['action_dispatch.show_exceptions'] == false
|
|
|
|
render_exception(env, exception)
|
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']
|
2013-08-21 08:42:04 -04:00
|
|
|
request = Request.new(env)
|
2011-12-01 14:46:18 -05:00
|
|
|
template = ActionView::Base.new([RESCUES_TEMPLATE_PATH],
|
2013-08-21 08:42:04 -04:00
|
|
|
request: request,
|
|
|
|
exception: wrapper.exception,
|
|
|
|
application_trace: wrapper.application_trace,
|
|
|
|
framework_trace: wrapper.framework_trace,
|
|
|
|
full_trace: wrapper.full_trace,
|
|
|
|
routes_inspector: routes_inspector(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}"
|
2013-08-21 08:42:04 -04:00
|
|
|
|
|
|
|
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)
|
2011-12-01 14:46:18 -05:00
|
|
|
else
|
|
|
|
raise exception
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|