class ActionDispatch::DebugExceptions

This middleware is responsible for logging exceptions and showing a debugging page in case the request is local.

Constants

RESCUES_TEMPLATE_PATH

Public Class Methods

new(app, routes_app = nil, response_format = :default) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 41
def initialize(app, routes_app = nil, response_format = :default)
  @app             = app
  @routes_app      = routes_app
  @response_format = response_format
end

Public Instance Methods

call(env) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 47
def call(env)
  request = ActionDispatch::Request.new env
  _, headers, body = response = @app.call(env)

  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}"
  end

  response
rescue Exception => exception
  raise exception unless request.show_exceptions?
  render_exception(request, exception)
end

Private Instance Methods

create_template(request, wrapper) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 120
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
log_array(logger, array) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 167
def log_array(logger, array)
  if logger.formatter && logger.formatter.respond_to?(:tags_text)
    logger.fatal array.join("\n#{logger.formatter.tags_text}")
  else
    logger.fatal array.join("\n")
  end
end
log_error(request, wrapper) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 149
def log_error(request, wrapper)
  logger = logger(request)
  return unless logger

  exception = wrapper.exception

  trace = wrapper.application_trace
  trace = wrapper.framework_trace if trace.empty?

  ActiveSupport::Deprecation.silence do
    logger.fatal "  "
    logger.fatal "#{exception.class} (#{exception.message}):"
    log_array logger, exception.annoted_source_code if exception.respond_to?(:annoted_source_code)
    logger.fatal "  "
    log_array logger, trace
  end
end
logger(request) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 175
def logger(request)
  request.logger || ActionView::Base.logger || stderr_logger
end
render(status, body, format) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 145
def render(status, body, format)
  [status, {'Content-Type' => "#{format}; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
end
render_exception(request, exception) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 64
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')
    case @response_format
    when :api
      render_for_api_application(request, wrapper)
    when :default
      render_for_default_application(request, wrapper)
    end
  else
    raise exception
  end
end
render_for_api_application(request, wrapper) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 95
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)
    formatted_body = body.public_send(to_format)
    format = content_type
  else
    formatted_body = body.to_json
    format = Mime[:json]
  end

  render(wrapper.status_code, formatted_body, format)
end
render_for_default_application(request, wrapper) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 81
def render_for_default_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
routes_inspector(exception) click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 183
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
end
stderr_logger() click to toggle source
# File lib/action_dispatch/middleware/debug_exceptions.rb, line 179
def stderr_logger
  @stderr_logger ||= ActiveSupport::Logger.new($stderr)
end