From 27839fead34ca062e5904864e7d1115eafd32b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 4 Jun 2013 17:15:08 +0200 Subject: [PATCH] [CLEAN] auth_signup, mail_mail: better management of exceptions when sending emails, delegated to mail_mail. bzr revid: tde@openerp.com-20130604151508-7bd8gxhkr86fuybp --- addons/auth_signup/res_users.py | 9 +-------- addons/mail/mail_mail.py | 17 ++++++++++------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/addons/auth_signup/res_users.py b/addons/auth_signup/res_users.py index 340865045aa..ad28bbb1d57 100644 --- a/addons/auth_signup/res_users.py +++ b/addons/auth_signup/res_users.py @@ -270,14 +270,7 @@ class res_users(osv.Model): raise osv.except_osv(_("Cannot send email: user has no email address."), user.name) try: self.pool.get('email.template').send_mail(cr, uid, template.id, user.id, force_send=True, raise_exception=True, context=context) - except AssertionError as e: - # get the args of the original error, wrap into a value and throw a MailDeliveryException - # that is an except_orm, with name and value as args - value = '. '.join(e.args) - if 'one valid recipient address should be specified' in e.args[0]: - value += '. Please check that the user\'s email address is valid.' - raise MailDeliveryException(_("Mail Delivery Failed"), value) - except MailDeliveryException: + except Exception: raise def create(self, cr, uid, values, context=None): diff --git a/addons/mail/mail_mail.py b/addons/mail/mail_mail.py index 4eaaa72971c..8fab4347d0d 100644 --- a/addons/mail/mail_mail.py +++ b/addons/mail/mail_mail.py @@ -27,6 +27,7 @@ from urlparse import urljoin from openerp import tools from openerp import SUPERUSER_ID +from openerp.addons.base.ir.ir_mail_server import MailDeliveryException from openerp.osv import fields, osv from openerp.tools.translate import _ @@ -304,8 +305,7 @@ class mail_mail(osv.Model): after sending each mail (meant only for scheduler processing); should never be True during normal transactions (default: False) :param bool raise_exception: whether to raise an exception if the - email sending process has failed; auto_commit is taken into - account. + email sending process has failed :return: True """ ir_mail_server = self.pool.get('ir.mail_server') @@ -351,12 +351,15 @@ class mail_mail(osv.Model): # see revid:odo@openerp.com-20120622152536-42b2s28lvdv3odyr in 6.1 if mail_sent: self._postprocess_sent_message(cr, uid, mail, context=context) - except Exception: - if not raise_exception or auto_commit: - _logger.exception('failed sending mail.mail %s', mail.id) - mail.write({'state': 'exception'}) - cr.commit() + except Exception as e: + _logger.exception('failed sending mail.mail %s', mail.id) + mail.write({'state': 'exception'}) if raise_exception: + if isinstance(e, AssertionError): + # get the args of the original error, wrap into a value and throw a MailDeliveryException + # that is an except_orm, with name and value as arguments + value = '. '.join(e.args) + raise MailDeliveryException(_("Mail Delivery Failed"), value) raise if auto_commit == True: