Class: HP::Cloud::ErrorResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/hpcloud/error_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.message
    rescue
      @error_string = error.to_s
    end
  end
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



26
27
28
# File 'lib/hpcloud/error_response.rb', line 26

def error
  @error
end

#error_stringObject (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

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/hpcloud/error_response.rb', line 79

def error_message_includes?(error, text)
  error_message = error.respond_to?(:response) ? parse_error(error.response) : error.message
  error_message.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
  message = 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
          message = v.to_s
          next
        end
        code = v["code"].to_s if v.has_key?("code")
        message = v["message"] if v.has_key?("message")
        details = nil
        details = v["details"] if v.has_key?("details")
      }
    rescue MultiJson::DecodeError => error
    end
  else
    message = "Unknown error response: " + response.to_s
  end
  ret += code + " " unless code.nil?
  ret += message unless message.nil?
  unless details.nil?
    ret += ": " unless ret.empty?
    ret += details
  end
  return ret
end

#to_sObject



84
85
86
# File 'lib/hpcloud/error_response.rb', line 84

def to_s
  @error_string
end