Created
March 11, 2021 12:08
-
-
Save denisolvr/d1620763bb131cd6b6771122bda75306 to your computer and use it in GitHub Desktop.
Python Wrapper Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import traceback | |
| import logging | |
| from datetime import datetime | |
| def exception_handler(func): | |
| tstart = datetime.now() | |
| def inner_function(*args, **kwargs): | |
| try: | |
| func(*args, **kwargs) | |
| except Exception as e: | |
| print(f"Process time: {datetime.now() - tstart}") | |
| logging.error(f"Fuction {func.__name__} has errors!") | |
| logging.error('Caught this error: ' + repr(e)) | |
| # logging.error(traceback.format_exc()) ## for more error details enable traceback | |
| else: | |
| # Exceting after a success | |
| print("Done!") | |
| finally: | |
| print(f"Starting Function {func.__name__} at {tstart}") | |
| # # Finally is called directly after executing the try statement whether an exception is thrown or not | |
| return inner_function | |
| @exception_handler # <<< adding the decorator before a function will execute the function inside our wrapper | |
| def test_func(): | |
| raise Exception("I am an error!") | |
| test_func() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment