Repository: rails/rails
File: actionpack/lib/action_dispatch/middleware/debug_exceptions.rb (Ruby)

Code before:
    private
      def invoke_interceptors(request, exception, wrapper)
        @interceptors.each do |interceptor|
          interceptor.call(request, exception)
        rescue Exception
          log_error(request, wrapper)
        end
      end

      def render_exception(request, exception, wrapper)
        log_error(request, wrapper)

        if request.get_header("action_dispatch.show_detailed_exceptions")
          begin
            content_type = request.formats.first
          rescue ActionDispatch::Http::MimeNegotiation::InvalidType
            content_type = Mime[:text]
          end

          begin
            head_request = request.head?
          rescue ActionController::UnknownHttpMethod
            head_request = false
          end

          if head_request
            render(wrapper.status_code, "", content_type)
          elsif api_request?(content_type)
            render_for_api_request(content_type, wrapper)
          else
            render_for_browser_request(request, wrapper, content_type)
          end
        else
          raise exception
        end
      end

      def render_for_browser_request(request, wrapper, content_type)
        template = create_template(request, wrapper)
        file = "rescues/#{wrapper.rescue_template}"

        if content_type == Mime[:md]
          body = template.render(template: file, layout: false, formats: [:text])
          format = "text/markdown"
        elsif 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

Review comment (suggestion):
I think we should just do `request.raw_request_method == "HEAD"`

Code after:
    private
      def invoke_interceptors(request, exception, wrapper)
        @interceptors.each do |interceptor|
          interceptor.call(request, exception)
        rescue Exception
          log_error(request, wrapper)
        end
      end

      def render_exception(request, exception, wrapper)
        log_error(request, wrapper)

        if request.get_header("action_dispatch.show_detailed_exceptions")
          begin
            content_type = request.formats.first
          rescue ActionDispatch::Http::MimeNegotiation::InvalidType
            content_type = Mime[:text]
          end

          if request.raw_request_method == "HEAD"
            render(wrapper.status_code, "", content_type)
          elsif api_request?(content_type)
            render_for_api_request(content_type, wrapper)
          else
            render_for_browser_request(request, wrapper, content_type)
          end
        else
          raise exception
        end
      end

      def render_for_browser_request(request, wrapper, content_type)
        template = create_template(request, wrapper)
        file = "rescues/#{wrapper.rescue_template}"

        if content_type == Mime[:md]
          body = template.render(template: file, layout: false, formats: [:text])
          format = "text/markdown"
        elsif 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

      def render_for_api_request(content_type, wrapper)
        body = {
          status: wrapper.status_code,