【问题标题】:Python, Azure Servicebus how to catch an exception handling?Python、Azure Servicebus如何捕捉异常处理?
【发布时间】:2020-04-07 12:56:04
【问题描述】:

我正在用 python 编写一个 azure 函数。收到消息后,我将转发 n 条消息。

我的代码是:

def sendToQueue(data):
    logging.info('sendToQueue: %s', data)  
    msg = Message(encodeData(data))
    d = json.loads(data)
    batchId = d['BatchId']
    logging.info('sending message with batch Id: %s', batchId)  
    try:
      logging.info('Sending oos message to queue %s', queue_oos_mssql_inbox)
      sbs.send_queue_message(queue_oos_mssql_inbox, msg)
      logging.info('Done Sending oos message to queue %s', queue_oos_mssql_inbox)
    except :
      logging.error('Unable to process message %s', batchId)
      errorDescription = 'Unable to sent message to ' + queue_oos_mssql_inbox
      logging.error('errorDescription message %s', errorDescription)
      error = createErrorMessage(batchId, '404', str(errorDescription))
      logging.error('error message %s', error)
      sendToErrorQueue(json.loads(error))

    logging.debug('done sending message: ') 

当它无法发送消息时,我想记录错误。我该如何做这样的事情:'除了 pyodbc.Error as ex:',但随后使用 ServiceBus.error?

我找不到任何示例或文档。

【问题讨论】:

    标签: python-3.x azure azure-functions azureservicebus


    【解决方案1】:

    解决方法比较简单,就是加了

    exception Exception as e:
        logging.error("Error msg: " + str(e))
    

    【讨论】:

      【解决方案2】:

      可以使用根错误类ServiceBusError

          try:
            logging.info('Sending oos message to queue %s', queue_oos_mssql_inbox)
            sbs.send_queue_message(queue_oos_mssql_inbox, msg)
            logging.info('Done Sending oos message to queue %s', queue_oos_mssql_inbox)
          except ServiceBusError as e:
            logging.error('Unable to process message %s', batchId)
            logging.exception('error %s', e)
      

      ServiceBusError:所有其他与服务总线相关的错误。它是上述所有错误的根源。

      或任何其他描述:

      https://pypi.org/project/azure-servicebus/

      或在参考文档中:

      https://azuresdkdocs.blob.core.windows.net/$web/python/azure-servicebus/latest/azure.servicebus.html#module-azure.servicebus.exceptions

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 2012-03-20
        • 1970-01-01
        • 2011-02-07
        • 1970-01-01
        • 2012-01-10
        • 2021-08-20
        • 2022-01-25
        • 2013-03-29
        相关资源
        最近更新 更多