<commit_before>module DropboxApiV2::Endpoints
  class Base
    def self.add_endpoint(name, &block)
      define_method(name, block)
      DropboxApiV2::Client.add_endpoint(name, self)
    end

    protected

    def run_request(params)
      @connection.run_request(self.class::Method, self.class::Path, params, {})
    end

    def perform_request(params)
      process_response(run_request(params))
    end

    def process_response(raw_response)
      case raw_response.status
      when 200, 409
        # Status code 409 is "Endpoint-specific error". We need to look at
        # the response body to build an exception.
        build_result(raw_response.env[:api_result])
      else
        raise raw_response.body
      end
    end

    def build_result(api_result)
      result_builder = DropboxApiV2::ResultBuilder.new(api_result)

      if result_builder.has_error?
        raise result_builder.build_error(self.class::ErrorType)
      else
        result_builder.build(self.class::ResultType)
      end
    end
  end
end<commit_msg>Improves the error message when we get an HTTP error response.<commit_after>module DropboxApiV2::Endpoints
  class Base
    def self.add_endpoint(name, &block)
      define_method(name, block)
      DropboxApiV2::Client.add_endpoint(name, self)
    end

    protected

    def run_request(params)
      @connection.run_request(self.class::Method, self.class::Path, params, {})
    end

    def perform_request(params)
      process_response(run_request(params))
    end

    def process_response(raw_response)
      case raw_response.status
      when 200, 409
        # Status code 409 is "Endpoint-specific error". We need to look at
        # the response body to build an exception.
        build_result(raw_response.env[:api_result])
      else
        raise "Error #{raw_response.status}: #{raw_response.body}"
      end
    end

    def build_result(api_result)
      result_builder = DropboxApiV2::ResultBuilder.new(api_result)

      if result_builder.has_error?
        raise result_builder.build_error(self.class::ErrorType)
      else
        result_builder.build(self.class::ResultType)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08064516129032258, 'fraction_numerical': 0.011059907834101382, 'mean_word_length': 3.027829313543599, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4211447', 'n_tokens_mistral': 666, 'n_tokens_neox': 637, 'n_words': 147}