class ActionDispatch::DebugExceptions - actionpack-4.2.4 Documentation

# 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) click to toggle source

    # File lib/action_dispatch/middleware/debug_exceptions.rb, line 11
    def initialize(app, routes_app = nil)
      @app        = app
      @routes_app = routes_app
    end

### Public Instance Methods

call(env) click to toggle source

    # File lib/action_dispatch/middleware/debug_exceptions.rb, line 16
    def call(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 if env['action_dispatch.show_exceptions'] == false
      render_exception(env, exception)
    end

### Private Instance Methods

log_error(env, wrapper) click to toggle source

    # File lib/action_dispatch/middleware/debug_exceptions.rb, line 79
    def log_error(env, wrapper)
      logger = logger(env)
      return unless logger

      exception = wrapper.exception

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

      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)
        message << "  " << trace.join("\n  ")
        logger.fatal("#{message}\n\n")
      end
    end

logger(env) click to toggle source

    # File lib/action_dispatch/middleware/debug_exceptions.rb, line 96
    def logger(env)
      env['action_dispatch.logger'] || stderr_logger
    end

render(status, body, format) click to toggle source

    # File lib/action_dispatch/middleware/debug_exceptions.rb, line 75
    def render(status, body, format)
      [status, {'Content-Type' => "#{format}; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
    end

render_exception(env, exception) click to toggle source

    # File lib/action_dispatch/middleware/debug_exceptions.rb, line 32
    def render_exception(env, exception)
      wrapper = ExceptionWrapper.new(env, exception)
      log_error(env, wrapper)

      if env['action_dispatch.show_detailed_exceptions']
        request = Request.new(env)
        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

        template = ActionView::Base.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(exception),
          source_extracts: wrapper.source_extracts,
          line_number: wrapper.line_number,
          file: wrapper.file
        )
        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)
      else
        raise exception
      end
    end

routes_inspector(exception) click to toggle source

    # File lib/action_dispatch/middleware/debug_exceptions.rb, line 104
    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 100
    def stderr_logger
      @stderr_logger ||= ActiveSupport::Logger.new($stderr)
    end