2

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

Peter Jordanson
  • 61
  • 2
  • 16

1 Answers1

0

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.

MangoNrFive
  • 1,541
  • 1
  • 10
  • 23