Skip to main content

Exception trap

Project description

exctrap: Trap exceptions for retry

It is not uncommon to have Python code that requires exceptions to be trapped, with the code triggering it retried, e.g., to handle temporary network or host failures when fetching web resources. There are quite a few Python packages that provide this functionality. Or actually, two functionalities: (1) trapping the exception, and (2) retrying the triggering code upon failure.

Typically, they work in the level of function: the code that needs to be retried is written as a function, and it is either wrapped within a context manager or passed to a retry function so that the function is invoked repeatedly until it succeeds or fail sufficiently many times. In this way, the two functionalities are packed into one function or context manager for the user.

We take a slightly different approach: the two functionalities are separated into two entities: a context manager that traps exception, and a function that returns the context manager. We argue that this leads to neater code.

Recipe

This provides most of what we normally need.

for etrapper in exctrap.trial():
    with etrapper:
        # Do whatever that may fail.
    # Will reach here whether or not an exception is raised.
    # If you want to reraise the exception here, use etrapper.reraise().
# If retry fails, the last exception is reraised so this is not reached.

Options

The trial() function provides all the options. These include:

  • num_tries: Maximum number of trials, if the code keeps raising exceptions for this many tries the exception is reraised without further retry.
  • retry_period: Number of seconds to wait between tries (adjusted by period_noise).
  • period_noise: Add or subtract at most this fraction of the retry_period to get the actual amount of seconds to sleep.
  • etypes: Exception types to trap. By default, trap all exceptions derived from Exception. This is passed to the constructor of ExcTrapper when creating exception trappers.

Note that this does not require any function to be created for the code needs to be exception-proof. Experience shows that code can be a lot more neat as a result: the code can access the required variables much more easily when it does not sit within a separate global function or an inner function.

Implementation

The "etrapper" is the context manager which traps exceptions when running under the "with" statement. This is done simply by recording exceptions in the trapper object, and returning True to swallow it, in the __exit__ function. If needed, the exception can be asked to be reraised().

The trial() function provides the retry logic and create exception trappers. Because the exception would be trapped in the trapper, all trial needs to do is to check whether an exception is trapped, and decide whether to retry or reraise.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exctrap-0.3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

exctrap-0.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file exctrap-0.3.tar.gz.

File metadata

  • Download URL: exctrap-0.3.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for exctrap-0.3.tar.gz
Algorithm Hash digest
SHA256 5d5ba685adf60dc6394e1831a1cc175a6adbd329eb8a0a1aba7b364ad2c54e17
MD5 b8bcb9cc5ae3d4e7e3c1475799719390
BLAKE2b-256 42686e6232aadd7d63b9782b8a1b46fe0a68ccc30b63f3f603ff707fe6d2e900

See more details on using hashes here.

File details

Details for the file exctrap-0.3-py3-none-any.whl.

File metadata

  • Download URL: exctrap-0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for exctrap-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7b96f376e6c8145db5f9a66929cd866a57a3a884330706855af0cf95d82cb433
MD5 6bbb3a5202fd836c25f526916d08f48d
BLAKE2b-256 4a4bd4e126659f09ff178d69384edf7ffe8419432f18d2f2f9bea5af95d760b7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page