Skip to content

Instantly share code, notes, and snippets.

@ArthurSteijn
Last active April 23, 2023 17:52
Show Gist options
  • Save ArthurSteijn/31723c5343dc9ce5e64b72c283c6f74e to your computer and use it in GitHub Desktop.
Save ArthurSteijn/31723c5343dc9ce5e64b72c283c6f74e to your computer and use it in GitHub Desktop.
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