How to handle Azure Python Function exception handling?

一个人想着一个人 提交于 2021-01-29 08:20:45

问题


I'm new to Python exception handling. How do I correctly try the following, except if .get_entity fails, but pass if Status 200?

Here is where I'm at:

  • Its not correct though. Hoping you could elaborate with an example.
from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity
from azure.common import AzureMissingResourceHttpError

def get_table_row(TableName, PartitionKey, RowKey):
    try:
        table_lookup = table_service.get_entity(TableName, PartitionKey, RowKey)
    except AzureMissingResourceHttpError as e:
        logging.error(f'#### Status Code: {e.status_code} ####')
    finally:
        if e.status_code == 200:
            return table_lookup
        else:
            logging.error(f'#### Status Code: {e.status_code} ####')

data = get_table_row(TableName, PartitionKey, RowKey)

回答1:


You can change your code like below:

def get_table_row(TableName, PartitionKey, RowKey):
    try:
        table_lookup = table_service.get_entity(TableName, PartitionKey, RowKey)
    except AzureMissingResourceHttpError as e:
        if e.status_code == 200:
            return table_lookup
        else:
            logging.error(f'#### Status Code: {e.status_code} ####')
            return "whatever you want"
    else:
        return table_lookup


来源:https://stackoverflow.com/questions/65129992/how-to-handle-azure-python-function-exception-handling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!