# Buggy code
def unhandled_error_observer(self, event):
    """
    This method is called when an unhandled error in Tribler is observed.
    It broadcasts the tribler_exception event.
    """
    if event["isError"]:
        text = ""
        if "log_legacy" in event and "log_text" in event:
            text = event["log_text"]
        elif "log_failure" in event:
            text = str(event["log_failure"])

        # There are some errors that we are ignoring.
        # No route to host: this issue is non-critical since Tribler can still function when a request fails.
        if "socket.error: [Errno 113]" in text:
            self._logger.error(
                "Observed no route to host error (but ignoring)."
                "This might indicate a problem with your firewall."
            )
            return

        # Socket block: this sometimes occurres on Windows and is non-critical.
        if "socket.error: [Errno %s]" % SOCKET_BLOCK_ERRORCODE in text:
            self._logger.error(
                "Unable to send data due to socket.error %s", SOCKET_BLOCK_ERRORCODE
            )
            return

        if "socket.error: [Errno 51]" in text:
            self._logger.error("Could not send data: network is unreachable.")
            return

        if "exceptions.ValueError: Invalid DNS-ID" in text:
            self._logger.error("Invalid DNS-ID")
            return

        # We already have a check for invalid infohash when adding a torrent, but if somehow we get this
        # error then we simply log and ignore it.
        if "exceptions.RuntimeError: invalid info-hash" in text:
            self._logger.error("Invalid info-hash found")
            return

        if self.lm.api_manager and len(text) > 0:
            self.lm.api_manager.root_endpoint.events_endpoint.on_tribler_exception(text)
            self.lm.api_manager.root_endpoint.state_endpoint.on_tribler_exception(text)


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/usr/share/tribler/TriblerGUI/event_request_manager.py", line 99, in on_read_data
raise RuntimeError(json_dict["event"]["text"])
RuntimeError: [Failure instance: Traceback: <class 'socket.error'>: [Errno 16] Device or resource busy
/usr/lib/python2.7/threading.py:801:__bootstrap_inner
/usr/lib/python2.7/threading.py:754:run
/usr/lib/python2.7/site-packages/twisted/_threads/_threadworker.py:46:work
/usr/lib/python2.7/site-packages/twisted/_threads/_team.py:190:doWork
--- <exception caught here> ---
/usr/lib/python2.7/site-packages/twisted/python/threadpool.py:250:inContext
/usr/lib/python2.7/site-packages/twisted/python/threadpool.py:266:<lambda>
/usr/lib/python2.7/site-packages/twisted/python/context.py:122:callWithContext
/usr/lib/python2.7/site-packages/twisted/python/context.py:85:callWithContext
/usr/lib/python2.7/site-packages/twisted/internet/_resolver.py:124:get
]

# Fixed code
def unhandled_error_observer(self, event):
    """
    This method is called when an unhandled error in Tribler is observed.
    It broadcasts the tribler_exception event.
    """
    if event["isError"]:
        text = ""
        if "log_legacy" in event and "log_text" in event:
            text = event["log_text"]
        elif "log_failure" in event:
            text = str(event["log_failure"])

        # There are some errors that we are ignoring.
        # No route to host: this issue is non-critical since Tribler can still function when a request fails.
        if "socket.error: [Errno 113]" in text:
            self._logger.error(
                "Observed no route to host error (but ignoring)."
                "This might indicate a problem with your firewall."
            )
            return

        # Socket block: this sometimes occurres on Windows and is non-critical.
        if "socket.error: [Errno %s]" % SOCKET_BLOCK_ERRORCODE in text:
            self._logger.error(
                "Unable to send data due to socket.error %s", SOCKET_BLOCK_ERRORCODE
            )
            return

        if "socket.error: [Errno 51]" in text:
            self._logger.error("Could not send data: network is unreachable.")
            return

        if "socket.error: [Errno 16]" in text:
            self._logger.error("Could not send data: socket is busy.")
            return

        if "exceptions.ValueError: Invalid DNS-ID" in text:
            self._logger.error("Invalid DNS-ID")
            return

        # We already have a check for invalid infohash when adding a torrent, but if somehow we get this
        # error then we simply log and ignore it.
        if "exceptions.RuntimeError: invalid info-hash" in text:
            self._logger.error("Invalid info-hash found")
            return

        if self.lm.api_manager and len(text) > 0:
            self.lm.api_manager.root_endpoint.events_endpoint.on_tribler_exception(text)
            self.lm.api_manager.root_endpoint.state_endpoint.on_tribler_exception(text)