
import json
import logging

from labonneboite.conf import settings


logger = logging.getLogger('main')


class MailNoSendException(Exception):
    pass

class EmailClient(object):
    to = settings.FORM_EMAIL
    from_email = settings.ADMIN_EMAIL
    subject = 'nouveau message entreprise LBB'


class MandrillClient(EmailClient):

    def __init__(self, mandrill):
        self.mandrill = mandrill

    def send(self, html):
        from_email = self.from_email
        to_email = self.to
        response = self.mandrill.send_email(
            subject=self.subject,
            to=[{'email': to_email}],
            html=html,
            from_email=from_email)
        content = json.loads(response.content.decode())
        if content[0]["status"] != "sent":
            raise MailNoSendException("email was not sent from %s to %s" % (from_email, to_email))
        return response


import json
import logging
from urllib.error import HTTPError

from labonneboite.conf import settings


logger = logging.getLogger('main')


class MailNoSendException(Exception):
    pass

class EmailClient(object):
    to = settings.FORM_EMAIL
    from_email = settings.ADMIN_EMAIL
    subject = 'nouveau message entreprise LBB'


class MandrillClient(EmailClient):

    def __init__(self, mandrill):
        self.mandrill = mandrill

    def send(self, html):
        from_email = self.from_email
        to_email = self.to

        try:
            response = self.mandrill.send_email(
                subject=self.subject,
                to=[{'email': to_email}],
                html=html,
                from_email=from_email)
            content = json.loads(response.content.decode())
            if content[0]["status"] != "sent":
                raise MailNoSendException("email was not sent from %s to %s" % (from_email, to_email))
        except HTTPError:
            raise MailNoSendException("email was not sent from %s to %s" % (from_email, to_email))

        return response

Handle HttpError when sending email
Handle HttpError when sending email

  
  import json
  import logging
+ from urllib.error import HTTPError
  
  from labonneboite.conf import settings
  
  
  logger = logging.getLogger('main')
  
  
  class MailNoSendException(Exception):
      pass
  
  class EmailClient(object):
      to = settings.FORM_EMAIL
      from_email = settings.ADMIN_EMAIL
      subject = 'nouveau message entreprise LBB'
  
  
  class MandrillClient(EmailClient):
  
      def __init__(self, mandrill):
          self.mandrill = mandrill
  
      def send(self, html):
          from_email = self.from_email
          to_email = self.to
+ 
+         try:
-         response = self.mandrill.send_email(
+             response = self.mandrill.send_email(
-             subject=self.subject,
+                 subject=self.subject,
-             to=[{'email': to_email}],
+                 to=[{'email': to_email}],
-             html=html,
+                 html=html,
-             from_email=from_email)
+                 from_email=from_email)
-         content = json.loads(response.content.decode())
+             content = json.loads(response.content.decode())
-         if content[0]["status"] != "sent":
+             if content[0]["status"] != "sent":
+                 raise MailNoSendException("email was not sent from %s to %s" % (from_email, to_email))
+         except HTTPError:
              raise MailNoSendException("email was not sent from %s to %s" % (from_email, to_email))
+ 
          return response
  
Handle HttpError when sending email
## Code Before:

import json
import logging

from labonneboite.conf import settings


logger = logging.getLogger('main')


class MailNoSendException(Exception):
    pass

class EmailClient(object):
    to = settings.FORM_EMAIL
    from_email = settings.ADMIN_EMAIL
    subject = 'nouveau message entreprise LBB'


class MandrillClient(EmailClient):

    def __init__(self, mandrill):
        self.mandrill = mandrill

    def send(self, html):
        from_email = self.from_email
        to_email = self.to
        response = self.mandrill.send_email(
            subject=self.subject,
            to=[{'email': to_email}],
            html=html,
            from_email=from_email)
        content = json.loads(response.content.decode())
        if content[0]["status"] != "sent":
            raise MailNoSendException("email was not sent from %s to %s" % (from_email, to_email))
        return response

## Instruction:
Handle HttpError when sending email
## Code After:

import json
import logging
from urllib.error import HTTPError

from labonneboite.conf import settings


logger = logging.getLogger('main')


class MailNoSendException(Exception):
    pass

class EmailClient(object):
    to = settings.FORM_EMAIL
    from_email = settings.ADMIN_EMAIL
    subject = 'nouveau message entreprise LBB'


class MandrillClient(EmailClient):

    def __init__(self, mandrill):
        self.mandrill = mandrill

    def send(self, html):
        from_email = self.from_email
        to_email = self.to

        try:
            response = self.mandrill.send_email(
                subject=self.subject,
                to=[{'email': to_email}],
                html=html,
                from_email=from_email)
            content = json.loads(response.content.decode())
            if content[0]["status"] != "sent":
                raise MailNoSendException("email was not sent from %s to %s" % (from_email, to_email))
        except HTTPError:
            raise MailNoSendException("email was not sent from %s to %s" % (from_email, to_email))

        return response
