This file contains 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 requests | |
def send_error_to_teams(error_message, webhook_url): | |
""" This function will use the requests library to make a POST request to the webhook URL with the error message as the payload """ | |
payload = { | |
"text": error_message | |
} | |
response = requests.post(webhook_url, json=payload) | |
response.raise_for_status() | |
def send_error_to_teams_decorator(webhook_url): | |
""" This function takes a function as its argument and returns a new function that wraps the original function. | |
Inside the wrapper function, you can catch any errors that occur and send them to the Teams channel using the send_error_to_teams function """ | |
def decorator(original_function): | |
def wrapper(*args, **kwargs): | |
try: | |
result = original_function(*args, **kwargs) | |
return result | |
except Exception as e: | |
error_message = f"Error in {original_function.__name__}: {str(e)}" | |
send_error_to_teams(error_message, webhook_url) | |
print(f"Error send to teams channel: {error_message}") | |
raise | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment