Class: Fog::Ecloud::Errors::ServiceError

Inherits:
Fog::Errors::Error
  • Object
show all
Defined in:
lib/fog/compute/ecloud/errors.rb

Overview

The parent class for all errors in the Fog::Compute::Ecloud module.

Direct Known Subclasses

Compute::Ecloud::ServiceError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#minor_error_codeObject (readonly)

Returns the value of attribute minor_error_code.



12
13
14
# File 'lib/fog/compute/ecloud/errors.rb', line 12

def minor_error_code
  @minor_error_code
end

#response_dataObject (readonly)

Returns the value of attribute response_data.



# File 'lib/fog/compute/ecloud/errors.rb', line 7

#status_codeInteger (readonly)

Returns the HTTP status code returned.

Returns:

  • (Integer)

    the HTTP status code returned



12
# File 'lib/fog/compute/ecloud/errors.rb', line 12

attr_reader :response_data, :status_code, :minor_error_code

Class Method Details

.extract_message(data) ⇒ String

Parse the response body for an error message

Parameters:

  • data (Hash)

    the decoded XML response

Returns:

  • (String)

    the error message, if found, otherwise the raw data



69
70
71
72
73
74
# File 'lib/fog/compute/ecloud/errors.rb', line 69

def self.extract_message(data)
  if data.is_a?(Hash)
    message = data[:message]
  end
  message || data.inspect
end

.extract_minor_code(data) ⇒ String

Parse the response body for the minor error code

Parameters:

  • data (Hash)

    the decoded XML response

Returns:

  • (String)

    the error minor error code, if found, otherwise nil



81
82
83
84
85
86
87
# File 'lib/fog/compute/ecloud/errors.rb', line 81

def self.extract_minor_code(data)
  minor_code = nil
  if data.is_a?(Hash)
    minor_code = data[:minorErrorCode]
  end
  minor_code
end

.slurp(error) ⇒ Object

Parse the response from the HTTP request to create a user friendly

message, including HTTP response code and error message (if any)

Parameters:

  • error (Object)

    the error object from the rescue block

Returns:

  • (Object)

    the new error object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fog/compute/ecloud/errors.rb', line 29

def self.slurp(error)
  data = nil
  message = nil
  status_code = nil
  minor_code = nil

  if error.response
    status_code = error.response.status
    unless error.response.body.empty?
      begin
        document = Fog::ToHashDocument.new
        parser = Nokogiri::XML::SAX::PushParser.new(document)
        parser << error.response.body
        parser.finish

        data = document.body

        message = extract_message(data)
        minor_code = extract_minor_code(data)

      rescue => e
        Fog::Logger.warning("Received exception '#{e}' while decoding: #{error.response.body}")
        message = error.response.body
        data = error.response.body
      end
    end
  end

  new_error = super(error, message)
  new_error.instance_variable_set(:@response_data, data)
  new_error.instance_variable_set(:@status_code, status_code)
  new_error.instance_variable_set(:@minor_error_code, minor_code)
  new_error
end

Instance Method Details

#to_sString

Make the HTTP status code pretty

Returns:

  • (String)

    the cleaned up status code



17
18
19
20
21
# File 'lib/fog/compute/ecloud/errors.rb', line 17

def to_s
  status = status_code ? "HTTP #{status_code}" : "HTTP <Unknown>"
  minor_code = minor_error_code ? minor_error_code : "Unknown"
  "[#{status} - #{minor_code}] #{super}"
end