Class: HP::Cloud::ErrorResponse
- Inherits:
-
Object
- Object
- HP::Cloud::ErrorResponse
- Defined in:
- lib/hpcloud/error_response.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#error_string ⇒ Object
readonly
Returns the value of attribute error_string.
Instance Method Summary collapse
-
#error_message_includes?(error, text) ⇒ Boolean
check to see if an error includes a particular text fragment.
-
#initialize(error) ⇒ ErrorResponse
constructor
A new instance of ErrorResponse.
-
#parse_error(response) ⇒ Object
pull the error message out of an JSON response.
- #to_s ⇒ Object
Constructor Details
#initialize(error) ⇒ ErrorResponse
Returns a new instance of ErrorResponse.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hpcloud/error_response.rb', line 28 def initialize(error) @error = error if (error.respond_to?(:response)) @error_string = parse_error(error.response) else begin @error_string = error. rescue @error_string = error.to_s end end end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
26 27 28 |
# File 'lib/hpcloud/error_response.rb', line 26 def error @error end |
#error_string ⇒ Object (readonly)
Returns the value of attribute error_string.
26 27 28 |
# File 'lib/hpcloud/error_response.rb', line 26 def error_string @error_string end |
Instance Method Details
#error_message_includes?(error, text) ⇒ Boolean
check to see if an error includes a particular text fragment
79 80 81 82 |
# File 'lib/hpcloud/error_response.rb', line 79 def (error, text) = error.respond_to?(:response) ? parse_error(error.response) : error. .include?(text) end |
#parse_error(response) ⇒ Object
pull the error message out of an JSON response
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/hpcloud/error_response.rb', line 42 def parse_error(response) ret = '' code = nil = nil details = nil if (response.respond_to?(:status)) code = response.status.to_s end if (response.respond_to?(:body)) details = response.body.to_s begin err_msg = MultiJson.decode(response.body) err_msg.map { |_,v| if ! v.kind_of? Hash = v.to_s next end code = v["code"].to_s if v.has_key?("code") = v["message"] if v.has_key?("message") details = nil details = v["details"] if v.has_key?("details") } rescue MultiJson::DecodeError => error end else = "Unknown error response: " + response.to_s end ret += code + " " unless code.nil? ret += unless .nil? unless details.nil? ret += ": " unless ret.empty? ret += details end return ret end |
#to_s ⇒ Object
84 85 86 |
# File 'lib/hpcloud/error_response.rb', line 84 def to_s @error_string end |