feat: Powiadomienia email o błędach 500

- Dodano send_error_notification() - wysyła email przy błędzie 500
- Szczegóły: URL, ścieżka, user, IP, traceback
- Konfiguracja: ERROR_NOTIFY_EMAIL, SMTP_HOST, SMTP_PORT
- Domyślnie: maciej.pienczyn@inpi.pl

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Maciej Pienczyn 2026-01-27 12:55:41 +01:00
parent 8823e5c961
commit 3584bba717

75
app.py
View File

@ -34,6 +34,10 @@ from werkzeug.security import generate_password_hash, check_password_hash
from dotenv import load_dotenv
from user_agents import parse as parse_user_agent
import uuid
import smtplib
import traceback as tb_module
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Load environment variables (override any existing env vars)
# Try .env first, then nordabiz_config.txt for production flexibility
@ -12590,8 +12594,79 @@ def not_found(error):
return render_template('errors/404.html'), 404
def send_error_notification(error, request_info):
"""Send email notification about 500 errors"""
try:
smtp_host = os.getenv('SMTP_HOST', 'localhost')
smtp_port = int(os.getenv('SMTP_PORT', 25))
mail_from = os.getenv('MAIL_FROM', 'noreply@nordabiznes.pl')
error_email = os.getenv('ERROR_NOTIFY_EMAIL', 'maciej.pienczyn@inpi.pl')
if not error_email:
return
# Build error details
error_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
traceback_str = tb_module.format_exc()
subject = f"🚨 NordaBiz ERROR 500: {request_info.get('path', 'Unknown')}"
body = f"""
BŁĄD 500 NA NORDABIZNES.PL
{'='*50}
🕐 Czas: {error_time}
🌐 URL: {request_info.get('url', 'N/A')}
📍 Ścieżka: {request_info.get('path', 'N/A')}
📝 Metoda: {request_info.get('method', 'N/A')}
👤 Użytkownik: {request_info.get('user', 'Anonimowy')}
🖥 IP: {request_info.get('ip', 'N/A')}
🌍 User-Agent: {request_info.get('user_agent', 'N/A')}
{'='*50}
📋 BŁĄD:
{str(error)}
{'='*50}
📜 TRACEBACK:
{traceback_str}
{'='*50}
🔧 Sprawdź logi: ssh maciejpi@10.22.68.249 "sudo journalctl -u nordabiznes --since '10 minutes ago'"
"""
msg = MIMEMultipart()
msg['From'] = mail_from
msg['To'] = error_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain', 'utf-8'))
with smtplib.SMTP(smtp_host, smtp_port, timeout=10) as server:
server.sendmail(mail_from, [error_email], msg.as_string())
logger.info(f"Error notification sent to {error_email}")
except Exception as e:
logger.error(f"Failed to send error notification: {e}")
@app.errorhandler(500)
def internal_error(error):
# Collect request info for notification
request_info = {
'url': request.url if request else 'N/A',
'path': request.path if request else 'N/A',
'method': request.method if request else 'N/A',
'ip': request.remote_addr if request else 'N/A',
'user_agent': request.headers.get('User-Agent', 'N/A') if request else 'N/A',
'user': current_user.email if current_user and current_user.is_authenticated else 'Anonimowy'
}
# Send notification in background (don't block response)
try:
send_error_notification(error, request_info)
except Exception as e:
logger.error(f"Error notification failed: {e}")
return render_template('errors/500.html'), 500