1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00
rest-client--rest-client/lib/request_errors.rb

32 lines
No EOL
807 B
Ruby

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