2017-07-01 16:09:13 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:47:31 -04:00
|
|
|
|
2017-10-21 09:18:17 -04:00
|
|
|
require "action_dispatch/middleware/exception_wrapper"
|
|
|
|
require "action_dispatch/routing/inspector"
|
2018-06-14 04:09:00 -04:00
|
|
|
|
2016-08-06 12:51:43 -04:00
|
|
|
require "action_view"
|
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
|
2016-02-24 16:19:57 -05:00
|
|
|
cattr_reader :interceptors, instance_accessor: false, default: []
|
|
|
|
|
|
|
|
def self.register_interceptor(object = nil, &block)
|
|
|
|
interceptor = object || block
|
|
|
|
interceptors << interceptor
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(app, routes_app = nil, response_format = :default, interceptors = self.class.interceptors)
|
2018-12-28 07:21:30 -05:00
|
|
|
@app = app
|
2015-07-20 15:38:09 -04:00
|
|
|
@routes_app = routes_app
|
|
|
|
@response_format = response_format
|
2016-02-24 16:19:57 -05:00
|
|
|
@interceptors = interceptors
|
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
|
|
|
|
2016-08-06 12:51:43 -04:00
|
|
|
if headers["X-Cascade"] == "pass"
|
2013-01-06 19:47:49 -05:00
|
|
|
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
|
2016-02-24 16:19:57 -05:00
|
|
|
invoke_interceptors(request, 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
|
2016-02-24 16:19:57 -05:00
|
|
|
def invoke_interceptors(request, exception)
|
|
|
|
backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner")
|
|
|
|
wrapper = ExceptionWrapper.new(backtrace_cleaner, exception)
|
|
|
|
|
|
|
|
@interceptors.each do |interceptor|
|
2018-12-20 12:44:01 -05:00
|
|
|
interceptor.call(request, exception)
|
|
|
|
rescue Exception
|
|
|
|
log_error(request, wrapper)
|
2016-02-24 16:19:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def render_exception(request, exception)
|
|
|
|
backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner")
|
|
|
|
wrapper = ExceptionWrapper.new(backtrace_cleaner, exception)
|
|
|
|
log_error(request, wrapper)
|
|
|
|
|
|
|
|
if request.get_header("action_dispatch.show_detailed_exceptions")
|
2019-03-25 16:51:22 -04:00
|
|
|
begin
|
|
|
|
content_type = request.formats.first
|
2020-10-07 10:48:54 -04:00
|
|
|
rescue ActionDispatch::Http::MimeNegotiation::InvalidType
|
2020-04-17 13:42:37 -04:00
|
|
|
content_type = Mime[:text]
|
2019-03-25 16:51:22 -04:00
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
|
|
|
|
if api_request?(content_type)
|
|
|
|
render_for_api_request(content_type, wrapper)
|
|
|
|
else
|
|
|
|
render_for_browser_request(request, wrapper)
|
|
|
|
end
|
2016-05-07 21:44:38 -04:00
|
|
|
else
|
2016-08-06 13:55:02 -04:00
|
|
|
raise exception
|
2013-08-21 08:42:04 -04:00
|
|
|
end
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def render_for_browser_request(request, wrapper)
|
|
|
|
template = create_template(request, wrapper)
|
|
|
|
file = "rescues/#{wrapper.rescue_template}"
|
2015-07-07 11:43:49 -04:00
|
|
|
|
2016-08-06 13:55:02 -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)
|
2015-07-07 11:43:49 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def render_for_api_request(content_type, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
to_format = "to_#{content_type.to_sym}"
|
|
|
|
|
|
|
|
if content_type && body.respond_to?(to_format)
|
|
|
|
formatted_body = body.public_send(to_format)
|
|
|
|
format = content_type
|
|
|
|
else
|
|
|
|
formatted_body = body.to_json
|
|
|
|
format = Mime[:json]
|
|
|
|
end
|
2015-07-07 11:43:49 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
render(wrapper.status_code, formatted_body, format)
|
2015-07-07 11:43:49 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def create_template(request, wrapper)
|
2018-06-14 04:09:00 -04:00
|
|
|
DebugView.new(
|
2016-08-06 13:55:02 -04:00
|
|
|
request: request,
|
2018-04-01 21:47:07 -04:00
|
|
|
exception_wrapper: wrapper,
|
2016-08-06 13:55:02 -04:00
|
|
|
exception: wrapper.exception,
|
2018-04-01 21:47:07 -04:00
|
|
|
traces: wrapper.traces,
|
|
|
|
show_source_idx: wrapper.source_to_show_id,
|
|
|
|
trace_to_show: wrapper.trace_to_show,
|
2016-08-06 13:55:02 -04:00
|
|
|
routes_inspector: routes_inspector(wrapper.exception),
|
|
|
|
source_extracts: wrapper.source_extracts,
|
|
|
|
line_number: wrapper.line_number,
|
|
|
|
file: wrapper.file
|
|
|
|
)
|
2015-07-07 11:43:49 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def render(status, body, format)
|
2016-08-16 03:30:11 -04:00
|
|
|
[status, { "Content-Type" => "#{format}; charset=#{Response.default_charset}", "Content-Length" => body.bytesize.to_s }, [body]]
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2011-12-01 14:46:18 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def log_error(request, wrapper)
|
|
|
|
logger = logger(request)
|
2017-06-22 11:52:53 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
return unless logger
|
2018-09-25 03:31:11 -04:00
|
|
|
return if !log_rescued_responses?(request) && wrapper.rescue_response?
|
2016-02-16 22:18:51 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
exception = wrapper.exception
|
2018-01-04 06:27:14 -05:00
|
|
|
trace = wrapper.exception_trace
|
2011-12-13 14:32:39 -05:00
|
|
|
|
2020-03-29 18:02:08 -04:00
|
|
|
message = []
|
|
|
|
message << " "
|
|
|
|
message << "#{exception.class} (#{exception.message}):"
|
|
|
|
message.concat(exception.annotated_source_code) if exception.respond_to?(:annotated_source_code)
|
|
|
|
message << " "
|
|
|
|
message.concat(trace)
|
|
|
|
|
|
|
|
log_array(logger, message)
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
|
2021-05-06 07:33:38 -04:00
|
|
|
def log_array(logger, lines)
|
2017-06-22 11:52:53 -04:00
|
|
|
return if lines.empty?
|
|
|
|
|
2016-08-19 10:08:26 -04:00
|
|
|
if logger.formatter && logger.formatter.respond_to?(:tags_text)
|
2017-06-22 11:52:53 -04:00
|
|
|
logger.fatal lines.join("\n#{logger.formatter.tags_text}")
|
2016-08-19 10:08:26 -04:00
|
|
|
else
|
2017-06-22 11:52:53 -04:00
|
|
|
logger.fatal lines.join("\n")
|
2016-08-19 10:08:26 -04:00
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2016-01-22 16:27:52 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def logger(request)
|
|
|
|
request.logger || ActionView::Base.logger || stderr_logger
|
|
|
|
end
|
2011-12-01 14:46:18 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def stderr_logger
|
|
|
|
@stderr_logger ||= ActiveSupport::Logger.new($stderr)
|
|
|
|
end
|
2012-06-09 15:54:49 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def routes_inspector(exception)
|
|
|
|
if @routes_app.respond_to?(:routes) && (exception.is_a?(ActionController::RoutingError) || exception.is_a?(ActionView::Template::Error))
|
|
|
|
ActionDispatch::Routing::RoutesInspector.new(@routes_app.routes.routes)
|
|
|
|
end
|
2013-01-05 07:59:00 -05:00
|
|
|
end
|
2016-05-07 21:44:38 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def api_request?(content_type)
|
|
|
|
@response_format == :api && !content_type.html?
|
|
|
|
end
|
2018-09-25 03:31:11 -04:00
|
|
|
|
|
|
|
def log_rescued_responses?(request)
|
2021-07-18 19:08:30 -04:00
|
|
|
request.get_header("action_dispatch.log_rescued_responses")
|
2018-09-25 03:31:11 -04:00
|
|
|
end
|
2011-12-01 14:46:18 -05:00
|
|
|
end
|
|
|
|
end
|