<commit_before>module SkroutzApi
  # = SkroutzApI Errors
  #
  # Generic SkroutzApi exception class.
  class SkroutzApiError < StandardError; end

  # Raised when SkroutzApi handles a non-successful response
  class ServerError < SkroutzApiError
    def initialize(status, body)
      super %Q(status: #{status}, body: "#{body}")
    end
  end

  # Raised when SkroutzApi can't parse the response for the resource
  class InvalidResource < SkroutzApiError
    def initialize(resource, body)
      super %Q(resource: #{resource}, body: "#{body}")
    end
  end

  # Raised when SkroutzApi doesn't find the requested resource
  class ResourceNotFound < SkroutzApiError
    def initialize(status, body)
      super %Q(status: #{status}, body: "#{body}")
    end
  end

  class TimeoutError < SkroutzApiError
    def initialize
      super 'The server did not produce a response within the configured time to wait'
    end
  end

  class ErrorHandler < Faraday::Response::Middleware
    def on_complete(env)
      case env[:status]
      when 404, 410
        raise SkroutzApi::ResourceNotFound.new(env.status, env.body)
      when 500...600
        raise SkroutzApi::ServerError.new(env.status, env.body)
      end
    end
  end
end<commit_msg>Add SkroutzApi::TimeoutHandler middleware<commit_after>module SkroutzApi
  # = SkroutzApI Errors
  #
  # Generic SkroutzApi exception class.
  class SkroutzApiError < StandardError; end

  # Raised when SkroutzApi handles a non-successful response
  class ServerError < SkroutzApiError
    def initialize(status, body)
      super %Q(status: #{status}, body: "#{body}")
    end
  end

  # Raised when SkroutzApi can't parse the response for the resource
  class InvalidResource < SkroutzApiError
    def initialize(resource, body)
      super %Q(resource: #{resource}, body: "#{body}")
    end
  end

  # Raised when SkroutzApi doesn't find the requested resource
  class ResourceNotFound < SkroutzApiError
    def initialize(status, body)
      super %Q(status: #{status}, body: "#{body}")
    end
  end

  class TimeoutError < SkroutzApiError
    def initialize
      super 'The server did not produce a response within the configured time to wait'
    end
  end

  class ErrorHandler < Faraday::Response::Middleware
    def on_complete(env)
      case env[:status]
      when 404, 410
        raise SkroutzApi::ResourceNotFound.new(env.status, env.body)
      when 500...600
        raise SkroutzApi::ServerError.new(env.status, env.body)
      end
    end
  end

  class TimeoutHandler < Faraday::Middleware
    def initialize(app)
      super(app)
    end

    def call(env)
      @app.call(env)
      rescue Faraday::TimeoutError
        raise SkroutzApi::TimeoutError
    end
  end
end
{'fraction_non_alphanumeric': 0.08232711306256861, 'fraction_numerical': 0.008781558726673985, 'mean_word_length': 3.2785602503912363, 'pattern_counts': {'":': 0, '<': 16, '<?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': '4006663', 'n_tokens_mistral': 879, 'n_tokens_neox': 814, 'n_words': 255}