当我使用nova.keypairs.create()
并将无效的公共密钥传递给它时,得到以下信息:
BadRequest: Keypair data is invalid: failed to generate fingerprint (HTTP 400) (Request-ID: req-12bc6440-f042-4687-9ee9-d89e7edc260d)
我尝试执行以下操作,并且出于明显的原因(这是OpenStack的唯一例外),此方法不起作用:
try:
nova.keypairs.create(name=keyname, public_key=key)
except BadRequest:
raise cherrypy.HTTPError(400, "Invalid public key")
如何在我自己的try和except语句中使用OpenStack特定的异常,例如
BadRequest
? 最佳答案
您将需要导入nova包的例外。通过github for the package,看来您需要执行以下操作:
from nova.exception import *
请注意,您看到的异常实际上是
InvalidKeypair
异常,它本身是异常类Invalid
的子类,BadRequest
消息只是它的模板文本。因此,完整的代码如下所示:
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")
关于python - 使用OpenStack Nova异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47693548/