import os
import sys

from pulse_actions.authentication import (get_user_and_password,
    AuthenticationError)

from mozillapulse.publishers import GenericPublisher
from mozillapulse.config import PulseConfiguration
from mozillapulse.messages.base import GenericMessage


class ExperimentalPublisher(GenericPublisher):
    def __init__(self, **kwargs):
        super(ExperimentalPublisher, self).__init__(
            PulseConfiguration(**kwargs),
            'exchange/adusca/experiment',
            **kwargs)


class MessageHandler:

    def __init__(self):
        """Create Publisher."""
        try:
            user, password = get_user_and_password()
        except AuthenticationError as e:
            print(e.message)
            sys.exit(1)
        self.publisher = ExperimentalPublisher(user=user, password=password)

    def publish_message(self, data, routing_key):
        """Publish a message to exchange/adusca/experiment."""
        msg = GenericMessage()
        msg.routing_parts = routing_key.split('.')
        for key, value in data.iteritems():
            msg.set_data(key, value)
        self.publisher.publish(msg)

import os
import sys

from pulse_actions.authentication import (
    get_user_and_password,
    AuthenticationError
)

from mozillapulse.publishers import GenericPublisher
from mozillapulse.config import PulseConfiguration
from mozillapulse.messages.base import GenericMessage


class ExperimentalPublisher(GenericPublisher):
    def __init__(self, **kwargs):
        super(ExperimentalPublisher, self).__init__(
            PulseConfiguration(**kwargs),
            'exchange/adusca/experiment',
            **kwargs)


class MessageHandler:

    def __init__(self):
        """Create Publisher."""
        try:
            user, password = get_user_and_password()
        except AuthenticationError as e:
            print(e.message)
            sys.exit(1)

        self.publisher = ExperimentalPublisher(user=user, password=password)

    def publish_message(self, data, routing_key):
        """Publish a message to exchange/adusca/experiment."""
        msg = GenericMessage()
        msg.routing_parts = routing_key.split('.')
        for key, value in data.iteritems():
            msg.set_data(key, value)
        try:
            self.publisher.publish(msg)
        except Exception as e:
            print('ERROR: We failed to post a pulse message with what we did')
            print(e.message)

Handle failing to publish to pulse
Handle failing to publish to pulse

  import os
  import sys
  
- from pulse_actions.authentication import (get_user_and_password,
+ from pulse_actions.authentication import (
+     get_user_and_password,
-     AuthenticationError)
+     AuthenticationError
+ )
  
  from mozillapulse.publishers import GenericPublisher
  from mozillapulse.config import PulseConfiguration
  from mozillapulse.messages.base import GenericMessage
  
  
  class ExperimentalPublisher(GenericPublisher):
      def __init__(self, **kwargs):
          super(ExperimentalPublisher, self).__init__(
              PulseConfiguration(**kwargs),
              'exchange/adusca/experiment',
              **kwargs)
  
  
  class MessageHandler:
  
      def __init__(self):
          """Create Publisher."""
          try:
              user, password = get_user_and_password()
          except AuthenticationError as e:
              print(e.message)
              sys.exit(1)
+ 
          self.publisher = ExperimentalPublisher(user=user, password=password)
  
      def publish_message(self, data, routing_key):
          """Publish a message to exchange/adusca/experiment."""
          msg = GenericMessage()
          msg.routing_parts = routing_key.split('.')
          for key, value in data.iteritems():
              msg.set_data(key, value)
+         try:
-         self.publisher.publish(msg)
+             self.publisher.publish(msg)
+         except Exception as e:
+             print('ERROR: We failed to post a pulse message with what we did')
+             print(e.message)
  
Handle failing to publish to pulse
## Code Before:
import os
import sys

from pulse_actions.authentication import (get_user_and_password,
    AuthenticationError)

from mozillapulse.publishers import GenericPublisher
from mozillapulse.config import PulseConfiguration
from mozillapulse.messages.base import GenericMessage


class ExperimentalPublisher(GenericPublisher):
    def __init__(self, **kwargs):
        super(ExperimentalPublisher, self).__init__(
            PulseConfiguration(**kwargs),
            'exchange/adusca/experiment',
            **kwargs)


class MessageHandler:

    def __init__(self):
        """Create Publisher."""
        try:
            user, password = get_user_and_password()
        except AuthenticationError as e:
            print(e.message)
            sys.exit(1)
        self.publisher = ExperimentalPublisher(user=user, password=password)

    def publish_message(self, data, routing_key):
        """Publish a message to exchange/adusca/experiment."""
        msg = GenericMessage()
        msg.routing_parts = routing_key.split('.')
        for key, value in data.iteritems():
            msg.set_data(key, value)
        self.publisher.publish(msg)

## Instruction:
Handle failing to publish to pulse
## Code After:
import os
import sys

from pulse_actions.authentication import (
    get_user_and_password,
    AuthenticationError
)

from mozillapulse.publishers import GenericPublisher
from mozillapulse.config import PulseConfiguration
from mozillapulse.messages.base import GenericMessage


class ExperimentalPublisher(GenericPublisher):
    def __init__(self, **kwargs):
        super(ExperimentalPublisher, self).__init__(
            PulseConfiguration(**kwargs),
            'exchange/adusca/experiment',
            **kwargs)


class MessageHandler:

    def __init__(self):
        """Create Publisher."""
        try:
            user, password = get_user_and_password()
        except AuthenticationError as e:
            print(e.message)
            sys.exit(1)

        self.publisher = ExperimentalPublisher(user=user, password=password)

    def publish_message(self, data, routing_key):
        """Publish a message to exchange/adusca/experiment."""
        msg = GenericMessage()
        msg.routing_parts = routing_key.split('.')
        for key, value in data.iteritems():
            msg.set_data(key, value)
        try:
            self.publisher.publish(msg)
        except Exception as e:
            print('ERROR: We failed to post a pulse message with what we did')
            print(e.message)
