Skip to content

Instantly share code, notes, and snippets.

@toby-p
Created December 18, 2018 19:44
Show Gist options
  • Save toby-p/0f4384d0707c68038d5c1114635c80ea to your computer and use it in GitHub Desktop.
Save toby-p/0f4384d0707c68038d5c1114635c80ea to your computer and use it in GitHub Desktop.
Python decorator to suppress errors.
# Global variable to determine whether or not to raise errors.
SUPPRESS_ERRORS = True
def suppress_errors(func):
"""Decorator function to suppress errors."""
if not SUPPRESS_ERRORS:
return func
def suppressed(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
return
return suppressed
# Test function:
@suppress_errors
def test(a, b):
return a*b
# Test execution of function. If `SUPPRESS_ERRORS` variable is set to True then
# nothing will happen; if set to False, an error is raised as normal.
test(2.5, "dspkfd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment