核心应用程序中未识别的核心应用程序中的自定义异常

发布于 2025-02-12 02:52:13 字数 819 浏览 1 评论 0原文

在我的核心应用程序中,我有一个自定义的异常“超时”,当被捕获时应返回消息并触发'sys.exit()'终止应用程序。

超时例外:

class timeout(Exception):
    def __init__(self, msg):
        super().__init__(msg)

核心应用程序:

try:
    s3Client.put_object(Bucket = bucket, Key = key, Body = body)
except timeout as error:
    print(error)
    sys.exit()

在上面的示例中,我使用的是boto3 AWS模块,但这可以用其他任何一个代替。在执行此BOTO3函数期间,核心应用程序将增加超时错误。

我期望的是,要在核心应用程序中提出超时错误(由信号模块FYI的警报触发),“要打印”,以及要触发的'sys.exit()' 。

取而代之的是,核心应用程序提高了自定义“ timeout()”异常,但是boto3函数未配置为处理它并返回httpclienterror('http client提出了一个未手动的例外:Integration timation timeout:intemant timeout')。我的核心应用程序依次不配置为处理“ httpclienterror()”,因此超时逻辑不会执行。

我希望在核心应用程序中处理我的“超时()”例外,即使模块抛出了不同的异常。

考虑修改模块以添加我的自定义异常,似乎是入侵的。

谢谢

In my core application I have a custom Exception 'timeout' which when caught should return a message and trigger 'sys.exit()' to terminate the application.

timeout Exception:

class timeout(Exception):
    def __init__(self, msg):
        super().__init__(msg)

Core Application:

try:
    s3Client.put_object(Bucket = bucket, Key = key, Body = body)
except timeout as error:
    print(error)
    sys.exit()

In the above example I am using boto3 AWS module, but this could be substituted with any other. During the execution of this boto3 function, the timeout error will be raised by the core application.

What I would expect, is for the timeout error to be raised (triggered by an Alarm from the Signal module FYI) in the Core Application, '' to be printed, and 'sys.exit()' to be triggered, terminating the application.

Instead, the Core Application raises the custom 'timeout()' exception, but the boto3 function is not configured to handle it and returns HTTPClientError('An HTTP Client raised an unhandled exception: Integration Timeout'). My Core Application in turn is not configured to handle 'HTTPClientError()', so the timeout logic does not execute.

I want a way for my 'timeout()' Exception to be handled in the Core Application, even if the module throws a different exception.

Considered modifying the module to add my custom Exception, seems hacky.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2025-02-19 02:52:14

有点骇客,但是如果您不想修改模块,这可能是一个选项:

超时例外:

class timeout(Exception):
    exception_identifier = "timeout"

    def __init__(self, msg):
        super().__init__(msg + f" exception_identifier={self.exception_identifier}")

核心应用程序:

import timeout


try:
    s3Client.put_object(Bucket = bucket, Key = key, Body = body)
except timeout as error:
    print(error)
    sys.exit()
except HTTPClientError as error:
    if f"exception_identifier={timeout.exception_identifier}" in error.msg:
        print(error)
        sys.exit()
    else:
        raise

也许有更好的方法来提取,如果最初的例外是超时,而不是比较字符串。您还可以使用诸如长ID弦之类的异常_识别器,这不太可能与消息中可能使用“超时”的其他例外冲突。

A bit hacky, but if you don't want to modify the module this could be an option:

timeout Exception:

class timeout(Exception):
    exception_identifier = "timeout"

    def __init__(self, msg):
        super().__init__(msg + f" exception_identifier={self.exception_identifier}")

Core Application:

import timeout


try:
    s3Client.put_object(Bucket = bucket, Key = key, Body = body)
except timeout as error:
    print(error)
    sys.exit()
except HTTPClientError as error:
    if f"exception_identifier={timeout.exception_identifier}" in error.msg:
        print(error)
        sys.exit()
    else:
        raise

Maybe there is a better way to extract if the original Exception was a timeout than comparing the strings. You can also use an exception_identifier like a long ID-string, that is unlikely to clash with other Exceptions that may use "timeout" in the message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文