diff --git a/gestion/mail/mail.py b/gestion/mail/mail.py index f76614a5..e0d00d8b 100644 --- a/gestion/mail/mail.py +++ b/gestion/mail/mail.py @@ -7,6 +7,8 @@ import json import inspect import locale import smtplib +import traceback +from contextlib import contextmanager from email.header import Header from email.message import Message @@ -189,6 +191,27 @@ def format_sender(sender, header_charset='utf-8'): return formataddr((sender_name, sender_addr)) +@contextmanager +def bugreport(): + try: + yield + except Exception as exc: + From = 'roots@crans.org' + to = From + tb = sys.exc_info()[2].tb_next + + data = { + 'from': From, + 'to': to, + 'exc': exc, + 'lineno': tb.tb_frame.f_lineno, + 'filename': os.path.basename(tb.tb_frame.f_code.co_filename), + 'traceback': traceback.format_exc(), + } + mail = generate('bugreport', data) + with ServerConnection() as conn: + conn.sendmail(From, [to], mail.as_string()) + raise class ServerConnection(object): """Connexion au serveur smtp""" diff --git a/gestion/mail/template/bugreport/From/fr b/gestion/mail/template/bugreport/From/fr new file mode 100644 index 00000000..f75e6e37 --- /dev/null +++ b/gestion/mail/template/bugreport/From/fr @@ -0,0 +1 @@ +{{from}} diff --git a/gestion/mail/template/bugreport/README b/gestion/mail/template/bugreport/README new file mode 100644 index 00000000..942e2226 --- /dev/null +++ b/gestion/mail/template/bugreport/README @@ -0,0 +1,2 @@ +Envoyé lorsqu'une exception a été jetée. Le mail contient le nom de l'exception +et un traceback. diff --git a/gestion/mail/template/bugreport/Subject/fr b/gestion/mail/template/bugreport/Subject/fr new file mode 100644 index 00000000..b96cfc46 --- /dev/null +++ b/gestion/mail/template/bugreport/Subject/fr @@ -0,0 +1 @@ +Bugreport: "{{ exc }}"{% if filename %} (in {{ filename }}+{{ lineno }} ){% endif %} diff --git a/gestion/mail/template/bugreport/To/fr b/gestion/mail/template/bugreport/To/fr new file mode 100644 index 00000000..e6199f75 --- /dev/null +++ b/gestion/mail/template/bugreport/To/fr @@ -0,0 +1 @@ +{{to}} diff --git a/gestion/mail/template/bugreport/X-Mailer/fr b/gestion/mail/template/bugreport/X-Mailer/fr new file mode 100644 index 00000000..f5d9c869 --- /dev/null +++ b/gestion/mail/template/bugreport/X-Mailer/fr @@ -0,0 +1 @@ +{{ mailer }} diff --git a/gestion/mail/template/bugreport/body/fr b/gestion/mail/template/bugreport/body/fr new file mode 100644 index 00000000..03512b0c --- /dev/null +++ b/gestion/mail/template/bugreport/body/fr @@ -0,0 +1,7 @@ +Une erreur est survenue {% if filename %} dans le fichier {{ filename }} (ligne {{ lineno }}){% endif %}. + +Traceback: +{{ traceback }} + +-- +bug-reporter