如何捕获 Key Vault SecretClient 错误 Azure Python SDK?
How to catch Key Vault SecretClient errors Azure Python SDK?
我是 Python 的新手。我有一个工作的整体程序,我想使用 try:
- except:
将其分解为单个函数 (def
) 来处理错误。
在创建 Key Vault 时捕获错误的方法有哪些SecretClient
?
尝试 1:
- 当我输入错误
kv_name
时这里没有任何反应。
- 我希望它记录指定的
except:
消息。
credentials = DefaultAzureCredential()
def create_kv_client(kv_name, credentials):
try:
kv_uri = 'https://' + kv_name + '.vault.azure.net'
kv_client = SecretClient(vault_url=kv_uri, credential=credentials)
except:
logging.error('####### Failed to create Key Vault Client #######')
return kv_client
kv_client = create_kv_client('notmykeyvaultname', credentials)
尝试 2:
- 当我输入错误时这里也没有任何反应
kv_name
- 我预计它会引发错误。
credentials = DefaultAzureCredential()
def create_kv_client(kv_name, credentials):
try:
kv_uri = 'https://' + kv_name + '.vault.azure.net'
kv_client = SecretClient(vault_url=kv_uri, credential=credentials)
except:
logging.error('####### Failed to create Key Vault Client #######')
return kv_client
kv_client = create_kv_client('notmykeyvaultname', credentials)
if kv_client == None:
raise Exception('Failed to create Key Vault Client')
尝试 3:
- 将
try:
- except:
移动到函数调用也没有触发日志消息
- 我希望
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
try:
kv_client = create_kv_client('notmykeyvaultname', credentials)
except:
logging.info('####### Failed to create Key Vault client #######')
完整性检查:
- 当我运行命令在函数之外时,失败
SecretClient
的输出不是None
。
- 我可以在这里挂钩什么来确定
SecretClient
是否已成功创建?
credentials = DefaultAzureCredential()
kv_uri = 'https://' + 'notmykeyvaultname' + '.vault.azure.net'
kv_client = SecretClient(vault_url=kv_uri, credential=credentials)
kv_client
`<azure.keyvault.secrets._client.SecretClient at 0x1512046b370>`
构造函数不检查给定的保险库是否存在,或者您是否可以访问它,因此在您的尝试中它成功了:您得到一个 SecretClient
实例,没有引发错误。
如果 URL 是一个不存在的保管库,或者您无权访问的保管库,您将在第一次尝试操作时看到错误:
>>> vault_url = "https://nonexistentvaultdoesnotexist.vault.azure.net"
>>> client = SecretClient(vault_url, DefaultAzureCredential())
>>> client.get_secret('secret_name')
Traceback (most recent call last):
...
azure.core.exceptions.ServiceRequestError: <urllib3.connection.VerifiedHTTPSConnection object at 0x000001E313946198>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
失败的操作会引发 azure-core 中定义的错误,您可以像往常一样使用 try/except 块处理这些错误。
我是 Python 的新手。我有一个工作的整体程序,我想使用 try:
- except:
将其分解为单个函数 (def
) 来处理错误。
在创建 Key Vault 时捕获错误的方法有哪些SecretClient
?
尝试 1:
- 当我输入错误
kv_name
时这里没有任何反应。 - 我希望它记录指定的
except:
消息。
credentials = DefaultAzureCredential()
def create_kv_client(kv_name, credentials):
try:
kv_uri = 'https://' + kv_name + '.vault.azure.net'
kv_client = SecretClient(vault_url=kv_uri, credential=credentials)
except:
logging.error('####### Failed to create Key Vault Client #######')
return kv_client
kv_client = create_kv_client('notmykeyvaultname', credentials)
尝试 2:
- 当我输入错误时这里也没有任何反应
kv_name
- 我预计它会引发错误。
credentials = DefaultAzureCredential()
def create_kv_client(kv_name, credentials):
try:
kv_uri = 'https://' + kv_name + '.vault.azure.net'
kv_client = SecretClient(vault_url=kv_uri, credential=credentials)
except:
logging.error('####### Failed to create Key Vault Client #######')
return kv_client
kv_client = create_kv_client('notmykeyvaultname', credentials)
if kv_client == None:
raise Exception('Failed to create Key Vault Client')
尝试 3:
- 将
try:
-except:
移动到函数调用也没有触发日志消息 - 我希望
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
try:
kv_client = create_kv_client('notmykeyvaultname', credentials)
except:
logging.info('####### Failed to create Key Vault client #######')
完整性检查:
- 当我运行命令在函数之外时,失败
SecretClient
的输出不是None
。 - 我可以在这里挂钩什么来确定
SecretClient
是否已成功创建?
credentials = DefaultAzureCredential()
kv_uri = 'https://' + 'notmykeyvaultname' + '.vault.azure.net'
kv_client = SecretClient(vault_url=kv_uri, credential=credentials)
kv_client
`<azure.keyvault.secrets._client.SecretClient at 0x1512046b370>`
构造函数不检查给定的保险库是否存在,或者您是否可以访问它,因此在您的尝试中它成功了:您得到一个 SecretClient
实例,没有引发错误。
如果 URL 是一个不存在的保管库,或者您无权访问的保管库,您将在第一次尝试操作时看到错误:
>>> vault_url = "https://nonexistentvaultdoesnotexist.vault.azure.net"
>>> client = SecretClient(vault_url, DefaultAzureCredential())
>>> client.get_secret('secret_name')
Traceback (most recent call last):
...
azure.core.exceptions.ServiceRequestError: <urllib3.connection.VerifiedHTTPSConnection object at 0x000001E313946198>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
失败的操作会引发 azure-core 中定义的错误,您可以像往常一样使用 try/except 块处理这些错误。