<commit_before>require 'rexml/document'

module RestClient
	# A redirect was encountered; caught by execute to retry with the new url.
	class Redirect < RuntimeError; end

	# Authorization is required to access the resource specified.
	class Unauthorized < RuntimeError; end

	# Request failed with an unhandled http error code.
	class RequestFailed < RuntimeError
		attr_accessor :response

		def initialize(response)
			@response = response
		end

		def http_code
			@response.code.to_i
		end

		def message(default = "Unknown error")
			return "Resource not found" if http_code == 404
			parse_error_xml rescue default
		end

		def parse_error_xml
			xml_errors = REXML::Document.new(@response.body).elements.to_a("//errors/error")
			xml_errors.empty? ? raise : xml_errors.map { |a| a.text }.join(" / ")
		end
	end
end

# for backwards compatibility
RestClient::Resource::Redirect = RestClient::Redirect
RestClient::Resource::Unauthorized = RestClient::Unauthorized
RestClient::Resource::RequestFailed = RestClient::RequestFailed<commit_msg>rdocs for RequestFailed<commit_after>require 'rexml/document'

module RestClient
	# A redirect was encountered; caught by execute to retry with the new url.
	class Redirect < RuntimeError; end

	# Authorization is required to access the resource specified.
	class Unauthorized < RuntimeError; end

	# The request failed, meaning the remote HTTP server returned a code other
	# than success, unauthorized, or redirect.
	#
	# The exception message attempts to extract the error from the XML, using
	# format returned by Rails: <errors><error>some message</error></errors>
	#
	# You can get the status code by e.http_code, or see anything about the
	# response via e.response.  For example, the entire result body (which is
	# probably an HTML error page) is e.response.body.
	class RequestFailed < RuntimeError
		attr_accessor :response

		def initialize(response)
			@response = response
		end

		def http_code
			@response.code.to_i
		end

		def message(default = "Unknown error")
			return "Resource not found" if http_code == 404
			parse_error_xml rescue default
		end

		def parse_error_xml
			xml_errors = REXML::Document.new(@response.body).elements.to_a("//errors/error")
			xml_errors.empty? ? raise : xml_errors.map { |a| a.text }.join(" / ")
		end
	end
end

# backwards compatibility
RestClient::Resource::Redirect = RestClient::Redirect
RestClient::Resource::Unauthorized = RestClient::Unauthorized
RestClient::Resource::RequestFailed = RestClient::RequestFailed
{'fraction_non_alphanumeric': 0.0845909451945989, 'fraction_numerical': 0.0023828435266084196, 'mean_word_length': 4.686230248306997, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '7281177', 'n_tokens_mistral': 764, 'n_tokens_neox': 701, 'n_words': 242}