0

I'm new to Python error handling.

Using Azure Table Storage as a config table for a Python Azure Function. To lookup a specific row in the table, I'm using the table_service constructor.

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)

If an entity (row) is not found, the result is:

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'
}

How do I use Python to access the Not Found message in the response?

ericOnline
  • 1,586
  • 1
  • 19
  • 54

1 Answers1

1

Try this:

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)

Result:

enter image description here

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16