Sha256: 81957889d5d4a7d6c046d8af7b60fd2a105ac05a8ef38bb45650da3ef60e3028

Contents?: true

Size: 943 Bytes

Versions: 6

Compression:

Stored size: 943 Bytes

Contents

# frozen_string_literal: true

module Itch
  # Mix in for request validation and response parsing
  module Request
    # rubocop:disable all
    def validate_response(page, action: "making request", content_type: "text/html")
      response_type = page.response["content-type"]
      return if page.code == "200" && response_type == content_type

      unless response_type == "application/json"
        raise Error, "Unexpected error occurred while #{action}: Response code #{page.code}"
      end

      error_data = nil
      begin
        data = JSON.parse(page.content)
        if data["errors"]
          error_data = data["errors"].respond_to?(:join) ? "\n#{data["errors"].join("\n")}" : data["errors"]
        end
      rescue StandardError
        raise Error, "Error parsing response while #{action}"
      end

      raise Error, "Unexpected error occurred while #{action}: #{error_data}"
    end
    # rubocop:enable all
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
itch_client-0.4.3 lib/itch/request.rb
itch_client-0.4.2 lib/itch/request.rb
itch_client-0.4.1 lib/itch/request.rb
itch_client-0.4.0 lib/itch/request.rb
itch_client-0.3.0 lib/itch/request.rb
itch_client-0.2.0 lib/itch/request.rb