Repository: puma/puma
File: lib/puma/server.rb (Ruby)

Code before:
        rescue StandardError => e
          @log_writer.unknown_error e, nil, "Client"
        end
      end
    end

    # Triggers a client timeout if the thread-pool shuts down
    # during execution of the provided block.
    def with_force_shutdown(client, &block)
      @thread_pool.with_force_shutdown(&block)
    rescue ThreadPool::ForceShutdown
      client.timeout!
    end

    # :nocov:

    # Handle various error types thrown by Client I/O operations.
    def client_error(e, client, requests = 1)
      # Swallow, do not log
      return if [ConnectionError, EOFError].include?(e.class)

      case e
      when MiniSSL::SSLError
        @log_writer.ssl_error e, client.io
      when HttpParserError
        status, headers, res_body = lowlevel_error(e, client.env, 400)
        prepare_response(status, headers, res_body, requests, client)

        client.write_error(400)
        @log_writer.parse_error e, client
      when HttpParserError501
        status, headers, res_body = lowlevel_error(e, client.env, 501)
        prepare_response(status, headers, res_body, requests, client)

        client.write_error(501)
        @log_writer.parse_error e, client
      else
        status, headers, res_body = lowlevel_error(e, client.env, 500)
        prepare_response(status, headers, res_body, requests, client)

        client.write_error(500)
        @log_writer.unknown_error e, nil, "Read"
      end
    end

    # A fallback rack response if +@app+ raises as exception.
    #
    def lowlevel_error(e, env, status=500)
      if handler = @options[:lowlevel_error_handler]
        if handler.arity == 1
          return handler.call(e)

Review comment (refactor):
Would you kindly refactor these lines into a method?

```ruby
def respond_to_error(status_code)
status, headers, res_body = lowlevel_error(e, client.env, status_code)
prepare_response(status, headers, res_body, requests, client)
client.write_error(status_code)
end
```

Code after:
      rescue StandardError => e
        client_error(e, client, requests)
        # The ensure tries to close +client+ down
        requests > 0
      ensure
        client.io_buffer.reset

        begin
          client.close if close_socket
        rescue IOError, SystemCallError
          Puma::Util.purge_interrupt_queue
          # Already closed
        rescue StandardError => e
          @log_writer.unknown_error e, nil, "Client"
        end
      end
    end

    # Triggers a client timeout if the thread-pool shuts down
    # during execution of the provided block.
    def with_force_shutdown(client, &block)
      @thread_pool.with_force_shutdown(&block)
    rescue ThreadPool::ForceShutdown
      client.timeout!
    end

    # :nocov:

    # Handle various error types thrown by Client I/O operations.
    def client_error(e, client, requests = 1)
      # Swallow, do not log
      return if [ConnectionError, EOFError].include?(e.class)

      case e
      when MiniSSL::SSLError
        lowlevel_error(e, client.env)
        @log_writer.ssl_error e, client.io
      when HttpParserError
        response_to_error(client, requests, e, 400)
        @log_writer.parse_error e, client
      when HttpParserError501
        response_to_error(client, requests, e, 501)
        @log_writer.parse_error e, client
      else
        response_to_error(client, requests, e, 500)
        @log_writer.unknown_error e, nil, "Read"
      end
    end

    # A fallback rack response if +@app+ raises as exception.
    #