1
0
mirror of https://gitlab.crans.org/bde/nk20-scripts synced 2024-12-26 01:12:25 +00:00

Merge branch 'master' of gitlab.crans.org:bde/nk20-scripts

This commit is contained in:
Pierre-antoine Comby 2020-06-01 17:57:27 +02:00
commit b58a643e0e
2 changed files with 0 additions and 42 deletions

View File

@ -7,7 +7,3 @@ from django.core.signals import got_request_exception
class ScriptsConfig(AppConfig):
name = 'scripts'
def ready(self):
from . import signals
got_request_exception.connect(signals.send_mail_on_exception)

View File

@ -1,38 +0,0 @@
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
import os
import sys
from django.conf import settings
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.views.debug import ExceptionReporter
def send_mail_on_exception(request, **kwargs):
"""
When an error occurs on the Note Kfet, a mail is automatically sent to the webmasters.
"""
if settings.DEBUG:
# Don't need to send a mail in debug mode, errors are already displayed in console and in the response view.
return
try:
exc_info = sys.exc_info()
exc_type = exc_info[0]
exc = exc_info[1]
tb = exc_info[2]
reporter = ExceptionReporter(request=request, exc_type=exc_type, exc_value=exc, tb=tb)
note_sender = os.getenv("NOTE_MAIL", "notekfet@example.com")
webmaster = os.getenv("WEBMASTER_MAIL", "notekfet@example.com")
message = render_to_string('scripts/mail-error500.txt', context={"error": reporter.get_traceback_text()})
message_html = render_to_string('scripts/mail-error500.html', context={"error": reporter.get_traceback_html()})
send_mail("Erreur dans la Note Kfet", message, note_sender, [webmaster], html_message=message_html)
except BaseException as e:
sys.stderr.write("Une erreur est survenue lors de l'envoi d'un mail, pour signaler une erreur.")
raise e