Skip to main content

Wrap functions that might fail some time, so it will retry to execute that function n times

Project description

Installation

pip3 install function_error_handling

About

The main use for this package is when making requests with the requests module. Sometimes the network might be unavailable, are the server (the endpiont) might be temporarily down, We don't want our script to quit just because of that (although we can put everything in a loop and catch error's that will still lose the progress of the previous loop, and is a bit messy).

This is where function_error_handling comes in, it gives you two very approachable techniques to solve this.

From here on I'll be using requests.get as the function we want to add error handling to.

Approach 1

Replacing requests.get with an alternitave error handled one.

import requests
import function_error_handling
requests.get = function_error_handling.wrap(requests.get)

Now when you call requests.get and it throws an error, we will catch that and try to run the request again - without you even knowing.

Approach 2

Decorating your own function.

import requests
import function_error_handling

@function_error_handling.wrap()
def requests_get(url):
    return requests.get(url)

Now when you call requests_get the same logic will be applied.

Options

The wrap function takes in a couple of arguments:

  • func: (callable) This is the function that we are adding error handling to.
  • number_of_attempts: (int) default=5 The number of times you want to retry before finally raising the error.
  • time_to_sleep: (int) default=30 Time to sleep between retries.
  • errors_to_catch: (tuple) default=(Exception, ) Which errors you want to handle.
  • validator: (callable) default=None A function that will validate if the return of the function is valid, if the function throws an error or returns false - it will be treated as if the func thew an error.
  • callback: (callable) default=None A function to be called each time an exception occurs, this function should take the exception as an argument.

More about validtators

Let's take another look at validators, because without them this whole thing is useless. A very common reason that a script will quit is when you are hitting a api endpiont, and you expect a certain data type, and you go ahead and call some function and that data that will give you an error when it doesn't understand that data it got, without validators there's no way for us to know that.

That's when we create a validator - when we expect the result of a given function to be something, but might a times be something else entirely. We create a function that takes in the func's return value and we check if it satisfies us, And if the validator function returns false or throws an error, then we retry, otherwise - we know we got the right data.

I have included some predefined validators which you can import like this: from function_error_handling import validators. Feel free to make pr with some other helpful validators.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

function_error_handling-2.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file function_error_handling-2.0-py3-none-any.whl.

File metadata

  • Download URL: function_error_handling-2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for function_error_handling-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ddfa11cbe1536a0e3001a47a3a7f3f48194138eed31ee36df0f51f65139e80d0
MD5 25fb443fbf4423f77e9d2573a5d0884e
BLAKE2b-256 3aa4d6ca736776b9469914e8c7e0acefa339142d867ffd0bc596b436885741db

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