Exception: RestClient::RequestFailed

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/request_errors.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ RequestFailed

Returns a new instance of RequestFailed.



31
32
33
# File 'lib/request_errors.rb', line 31

def initialize(response=nil)
	@response = response
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



29
30
31
# File 'lib/request_errors.rb', line 29

def response
  @response
end

Instance Method Details

#http_codeObject



35
36
37
# File 'lib/request_errors.rb', line 35

def http_code
	@response.code.to_i if @response
end

#message(default = "Unknown error, HTTP status code #{http_code}") ⇒ Object



39
40
41
42
# File 'lib/request_errors.rb', line 39

def message(default="Unknown error, HTTP status code #{http_code}")
	return default unless @response
	parse_error_xml rescue default
end

#parse_error_xmlObject



44
45
46
47
# File 'lib/request_errors.rb', line 44

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

#to_sObject



49
50
51
# File 'lib/request_errors.rb', line 49

def to_s
	message
end