如何挂接到 Azure Python 函数中的 Azure TableService Response?

How to hook into Azure TableService Response in Azure Python Function?

我是 Python 错误处理的新手。

使用 Azure Table 存储作为 table Python Azure 函数的配置。要查找 table 中的特定行,我使用 table_service 构造函数。

from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity

TableName = "Table1"
PartitionKey = 'test'
RowKey = '123'

table_service.get_entity(TableName, PartitionKey, RowKey)

如果找到实体(行),则结果为:

Client-Request-ID = 123-xzy 
Receiving Response: Server
Timestamp = Thu, 19 Nov 2020 20: 43: 23 GMT,
Server-Request-ID = xyz-123,
HTTP Status Code = 404,
Message = Not Found,
Headers = {
    'cache-control': 'no-cache', 
    'transfer-encoding': 'chunked', 
    'content-type': 'application/json;odata=minimalmetadata;streaming=true;charset=utf-8', 
    'server': 'Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0', 
    'x-ms-request-id': 'xxx-xxx', 
    'x-ms-version': '2018-03-28', 
    'x-content-type-options': 'nosniff', 
    'date': 'Thu, 19 Nov 2020 20:43:23 GMT'
}

如何使用 Python 访问响应中的 Not Found 消息?

试试这个:

from azure.common import AzureMissingResourceHttpError

try:
  table_service.get_entity(TableName, PartitionKey, RowKey)
except AzureMissingResourceHttpError as e:
  exception_content = e.args[0]
  print("===values you need===")
  print(exception_content[:exception_content.find("\n")])
  print("===all exception content===")
  print(e.status_code)
  print(exception_content)

结果: