python在函数整体加except_如何将Azure Python SDK异常添加到try / except语句?

I'm new to Python. I have a working, monolithic Python program that I'm breaking into individual Python functions. I'd like to use the try: - except: pattern to catch specific exceptions for each function.

Example: Create a Key Vault client and retrieve a secret from Key Vault

import logging

from azure.identity import DefaultAzureCredential

from azure.keyvault.secrets import SecretClient

credentials = DefaultAzureCredential()

def create_kv_client(kv_name, credentials):

kv_uri = 'https://' + kv_name + '.vault.azure.net'

kv_client = SecretClient(vault_url=kv_uri, credential=credentials)

return kv_client

kv_client = create_kv_client('mykeyvaultname', credentials)

def retrieve_secret(table_stg_acct_key, kv_client):

retrieved_account_key = kv_client.get_secret(table_stg_acct_key)

return retrieved_account_key

try:

retrieved_account_key = retrieve_secret('mykeyvaultsecretname', kv_client)

print(retrieved_account_key)

except:

logging.error('####### Failed to retrieve key from Key Vault #######')

raise BaseException

Rather than raise BaseException here, I'd like to use the Azure Core exceptions module and log the actual message in the exception.

How is the except: statement handled in the case where two exceptions could be raised?

Example: There could be two exceptions raised by the get_secret method.

If the Key Vault URL is incorrect, a ServiceRequestError is raised:

ServiceRequestError: : Failed to establish a new connection: [Errno 11001] getaddrinfo failed

If the Key Vault secret name is incorrect, a ResourceNotFoundError is raised:

ResourceNotFoundError: (SecretNotFound) A secret with (name/id) notmykeyvaultsecretname was not found in this key vault. If you recently deleted this secret you may be able to recover it using the correct recovery command. For help resolving this issue, please see https://go.microsoft.com/fwlink/?linkid=2125182

How is this accomplished?

Do I have to import the azure core exception module?

An example of this pattern would be very helpful.

解决方案

The exception will be caught in order of "except" clauses, but beware of the subclass tree, since a except will catch all subclasses as well. For instance, this one leads to unreachable code.

try:

# do something

except BaseException:

# do something with

except DerivedException:

# assuming DerivedException is an extension of BaseException, you can't reach that code

So put them in most specific first.

In your Azure situation, this brings to something like:

from azure.core.exceptions import (

ServiceRequestError,

ResourceNotFoundError,

AzureError

)

try:

# do KV stuff

except ServiceRequestError:

# Network error, I will let it raise to higher level

raise

except ResourceNotFoundError:

# Let's assume it's not big deal here, just let it go

pass

except AzureError as e:

# Will catch everything that is from Azure SDK, but not the two previous

logger.critical("Azure SDK was not able to deal with my query", e)

raise

except Exception as e:

# Anything else that is not Azure related (network, stdlib, etc.)

logger.critical("Unknown error I can't blame Azure for", e)

raise

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值