Search code examples
pythonexceptionopenstackopenstack-nova

Using OpenStack Nova Exceptions


When I'm using nova.keypairs.create() and I pass it an invalid public key, I get the following:

BadRequest: Keypair data is invalid: failed to generate fingerprint (HTTP 400) (Request-ID: req-12bc6440-f042-4687-9ee9-d89e7edc260d)

I tried doing the following and for obvious reasons (it's a unique exception to OpenStack) it didn't work:

try:
    nova.keypairs.create(name=keyname, public_key=key)
except BadRequest:
    raise cherrypy.HTTPError(400, "Invalid public key")

How can I use OpenStack specific exceptions such as BadRequest within my own try and except statements?


Solution

  • You will need to import the exceptions for nova package. Going through github for the package, it looks like you will need to do:

    from nova.exception import *
    

    Note that the exception you are seeing is actually InvalidKeypair exception, which itself subclasses from exception class Invalid, the BadRequest message is just the template text for it.

    So, your complete code would look something like:

    from nova.exception import *
    # You can import specific ones if you are confident about them
    try:
        nova.keypairs.create(name=keyname, public_key=key)
    except InvalidKeypair:
        raise cherrypy.HTTPError(400, "Invalid public key")