30 lines
1.2 KiB
Python
30 lines
1.2 KiB
Python
import requests
|
|
from flask import current_app
|
|
|
|
def send_error_webhook(error_message, error_type, stack_trace):
|
|
webhook_url = current_app.config.get('ERROR_WEBHOOK_URL')
|
|
if not webhook_url:
|
|
current_app.logger.warning("ERROR_WEBHOOK_URL not set. Skipping webhook.")
|
|
return
|
|
|
|
# エラーメッセージを整形
|
|
formatted_message = f"**Error Type:** {error_type}\n**Message:** {error_message}\n```\n{stack_trace[:1500]}...\n```"
|
|
|
|
# ディスコードのWebhook形式に合わせてペイロードを構築
|
|
payload = {
|
|
"embeds": [{
|
|
"title": "Error Details",
|
|
"description": formatted_message,
|
|
"color": 15158332 # 赤色
|
|
}]
|
|
}
|
|
|
|
try:
|
|
current_app.logger.debug(f"Sending webhook to URL: {webhook_url}")
|
|
current_app.logger.debug(f"Webhook payload: {payload}")
|
|
|
|
response = requests.post(webhook_url, json=payload)
|
|
response.raise_for_status()
|
|
current_app.logger.info(f"Error webhook sent successfully to Discord. Status code: {response.status_code}")
|
|
except requests.RequestException as e:
|
|
current_app.logger.error(f"Failed to send error webhook to Discord: {str(e)}") |