# Buggy code
def publish(self):
    if not dbus:
        logger.debug("Zeroconf publish failed: dbus not installed.")
        return False

    try:
        bus = dbus.SystemBus()
    except dbus.exceptions.DBusException as e:
        logger.debug("Zeroconf publish failed: %s", e)
        return False

    if not bus.name_has_owner("org.freedesktop.Avahi"):
        logger.debug("Zeroconf publish failed: Avahi service not running.")
        return False

    server = dbus.Interface(
        bus.get_object("org.freedesktop.Avahi", "/"), "org.freedesktop.Avahi.Server"
    )

    self.group = dbus.Interface(
        bus.get_object("org.freedesktop.Avahi", server.EntryGroupNew()),
        "org.freedesktop.Avahi.EntryGroup",
    )

    text = [_convert_text_to_dbus_bytes(t) for t in self.text]
    self.group.AddService(
        _AVAHI_IF_UNSPEC,
        _AVAHI_PROTO_UNSPEC,
        dbus.UInt32(_AVAHI_PUBLISHFLAGS_NONE),
        self.name,
        self.stype,
        self.domain,
        self.host,
        dbus.UInt16(self.port),
        text,
    )

    self.group.Commit()
    return True


# Traceback (DBusException)
INFO     2013-11-13 23:26:28,001 [4304:MainThread] mopidy.frontends.mpd
MPD server running at [::]:6600
ERROR    2013-11-13 23:26:28,013 [4304:MpdFrontend-5] pykka
Unhandled exception in MpdFrontend (urn:uuid:93b3b156-44ca-42de-a0cf-602474d1c582):
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Pykka-1.2.0-py2.7.egg/pykka/actor.py", line 191, in _actor_loop
self.on_start()
File "/home/zenith/dev/mopidy/mopidy/frontends/mpd/actor.py", line 49, in on_start
if self.zeroconf_service.publish():
File "/home/zenith/dev/mopidy/mopidy/utils/zeroconf.py", line 73, in publish
dbus.UInt16(self.port), text)
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 70, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
DBusException: org.freedesktop.Avahi.InvalidHostNameError: Invalid host name

# Fixed code
def publish(self):
    if not dbus:
        logger.debug("Zeroconf publish failed: dbus not installed.")
        return False

    try:
        bus = dbus.SystemBus()
    except dbus.exceptions.DBusException as e:
        logger.debug("Zeroconf publish failed: %s", e)
        return False

    if not bus.name_has_owner("org.freedesktop.Avahi"):
        logger.debug("Zeroconf publish failed: Avahi service not running.")
        return False

    server = dbus.Interface(
        bus.get_object("org.freedesktop.Avahi", "/"), "org.freedesktop.Avahi.Server"
    )

    self.group = dbus.Interface(
        bus.get_object("org.freedesktop.Avahi", server.EntryGroupNew()),
        "org.freedesktop.Avahi.EntryGroup",
    )

    try:
        text = [_convert_text_to_dbus_bytes(t) for t in self.text]
        self.group.AddService(
            _AVAHI_IF_UNSPEC,
            _AVAHI_PROTO_UNSPEC,
            dbus.UInt32(_AVAHI_PUBLISHFLAGS_NONE),
            self.name,
            self.stype,
            self.domain,
            self.host,
            dbus.UInt16(self.port),
            text,
        )
    except dbus.exceptions.DBusException as e:
        logger.debug("Zeroconf publish failed: %s", e)
        return False

    self.group.Commit()
    return True