class CloudServers::Exception

Public Class Methods

raise_exception(response) click to toggle source

In the event of a non-200 HTTP status code, this method takes the HTTP response, parses the JSON from the body to get more information about the exception, then raises the proper error. Note that all exceptions are scoped in the CloudServers::Exception namespace.

# File lib/cloudservers/exception.rb, line 68
def self.raise_exception(response)
  return if response.code =~ /^20.$/
  begin
    fault = nil
    info = nil
    JSON.parse(response.body).each_pair do |key, val|
        fault=key
        info=val
    end
    exception_class = self.const_get(fault[0,1].capitalize+fault[1,fault.length])
    raise exception_class.new(info["message"], response.code, response.body)
  rescue NameError
    raise CloudServers::Exception::Other.new("The server returned status #{response.code}", response.code, response.body)
  end
end