【问题标题】:Exception handling in aws-lambda functionsaws-lambda 函数中的异常处理
【发布时间】:2019-01-27 10:41:48
【问题描述】:

在尝试实现DNSRequest 时,我还需要进行一些异常处理并注意到一些奇怪的东西。以下代码能够捕获 DNS 请求 timeouts

def lambda_handler(event, context):

    hostname = "google.de"
    dnsIpAddresses = event['dnsIpAddresses']
    dnsResolver = dns.resolver.Resolver()
    dnsResolver.lifetime = 1.0
    result = {}

    for dnsIpAddress in dnsIpAddresses:
        dnsResolver.nameservers = [dnsIpAddress]

        try:
           myAnswers = dnsResolver.query(hostname, "A")
           print(myAnswers)
           result[dnsIpAddress] = "SUCCESS"
        except dns.resolver.Timeout:
           print("caught Timeout exception")
           result[dnsIpAddress] = "FAILURE"
        except dns.exception.DNSException:
           print("caught DNSException exception")
           result[dnsIpAddress] = "FAILURE"
        except:
           result[dnsIpAddress] = "FAILURE"
           print("caught general exception")

    return result

现在,如果我删除了 Timeout 块,并假设会发生 Timeout,则会在 DNSException 上显示消息

捕获 DNSException 异常

永远不会显示。

现在,如果我删除了 DNSException 块,并假设会发生超时,则消息

捕获一般异常

永远不会显示。

但是 Timeout 扩展了 DNSException,而 DNSException 扩展了 Exception。我期望至少一般的期望块应该起作用。

我错过了什么?

【问题讨论】:

    标签: python python-3.x amazon-web-services dns aws-lambda


    【解决方案1】:

    在您的最后一个块中,尝试将 print 行放在 result[dnsIpAddress] = "FAILURE" 之前 我的猜测是代码比这里显示的多,或者 print 语句之前的行会导致不同的异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      相关资源
      最近更新 更多