Class: OpenStack::Exception

Inherits:
Object
  • Object
show all
Defined in:
lib/openstack/connection.rb

Overview

OpenStack::Exception

Defined Under Namespace

Classes: Authentication, BackupOrResizeInProgress, BadMediaType, BadMethod, BadRequest, BuildInProgress, ComputeError, ComputeFault, Connection, ExpiredAuthToken, InvalidArgument, ItemNotFound, MissingArgument, NotImplemented, Other, OverLimit, PersonalityFilePathTooLong, PersonalityFileTooLarge, ResizeNotAllowed, ResourceStateConflict, ServerCapacityUnavailable, ServiceUnavailable, TooManyPersonalityItems, Unauthorized

Class Method Summary collapse

Class Method Details

.deal_with_faulty_error(response, parse_error) ⇒ Object

e.g. os.delete(“non-existant”) ==> response.body is: “404 Not FoundnnThe resource could not be found.nn ” which doesn’t parse. Deal with such cases here if possible (JSON::ParserError)

Raises:



471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/openstack/connection.rb', line 471

def self.deal_with_faulty_error(response, parse_error)
  case response.code
  when "404"
    klass = self.const_get("ItemNotFound")
    msg = "The resource could not be found"
  when "409"
    klass = self.const_get("ResourceStateConflict")
    msg = "There was a conflict with the state of the resource"
  else
    klass = self.const_get("Other")
    msg = "Oops - not sure what happened: #{parse_error}"
  end
  raise klass.new(msg, response.code.to_s, response.body)
end

.raise_exception(response) ⇒ Object

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 OpenStack::Compute::Exception namespace.



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/openstack/connection.rb', line 443

def self.raise_exception(response)
  return if response.code =~ /^20.$/
  begin
    fault = nil
    info = nil
    if response.body.nil? && response.code == "404" #HEAD ops no body returned
      exception_class = self.const_get("ItemNotFound")
      raise exception_class.new("The resource could not be found", "404", "")
    else
      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)
    end
  rescue JSON::ParserError => parse_error
      deal_with_faulty_error(response, parse_error)
  rescue NameError
    raise OpenStack::Exception::Other.new("The server returned status #{response.code}", response.code, response.body)
  end
end