mail: ajout context manager "bugreport"

This commit is contained in:
Daniel STAN 2014-11-28 18:20:08 +01:00
parent be3f3507cc
commit 83283fc2cb
7 changed files with 36 additions and 0 deletions

View file

@ -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"""

View file

@ -0,0 +1 @@
{{from}}

View file

@ -0,0 +1,2 @@
Envoyé lorsqu'une exception a été jetée. Le mail contient le nom de l'exception
et un traceback.

View file

@ -0,0 +1 @@
Bugreport: "{{ exc }}"{% if filename %} (in {{ filename }}+{{ lineno }} ){% endif %}

View file

@ -0,0 +1 @@
{{to}}

View file

@ -0,0 +1 @@
{{ mailer }}

View file

@ -0,0 +1,7 @@
Une erreur est survenue {% if filename %} dans le fichier {{ filename }} (ligne {{ lineno }}){% endif %}.
Traceback:
{{ traceback }}
--
bug-reporter