# Buggy code
def check_new_version(self):
    def parse_body(body):
        if body is None:
            return
        version = json.loads(body)["name"][1:]
        if LooseVersion(version) > LooseVersion(version_id):
            self.session.notifier.notify(NTFY_NEW_VERSION, NTFY_INSERT, None, version)

    def on_request_error(error):
        error.trap(SchemeNotSupported, ConnectError, DNSLookupError)
        self._logger.error("Error when performing version check request: %s", error)

    def parse_response(response):
        if response.code == 200:
            return readBody(response)
        else:
            self._logger.warning(
                "Got response code %s when performing version check request",
                response.code,
            )

    agent = Agent(reactor)
    return (
        agent.request(
            "GET",
            VERSION_CHECK_URL,
            Headers({"User-Agent": ["Tribler " + version_id]}),
            None,
        )
        .addCallback(parse_response)
        .addCallback(parse_body)
        .addErrback(on_request_error)
    )


# Traceback (AssertionError)
I: test_add_torrent_from_magnet_to_channel_without_description (Tribler.Test.Core.Modules.RestApi.Channels.test_channels_torrents_endpoint.TestModifyChannelTorrentEndpoint) ... ok
I: test_add_torrent_from_url_to_channel_with_description (Tribler.Test.Core.Modules.RestApi.Channels.test_channels_torrents_endpoint.TestModifyChannelTorrentEndpoint) ... 2016-07-04 16:21:10,891 [ERROR] The reactor was dirty:
I: 2016-07-04 16:21:10,894 [ERROR] >     <DelayedCall 0x9792a968 [0.772477149963s] called=0 cancelled=0 LoopingCall<5>(ChannelCommunity.take_step, *(), **{})()>
I: 2016-07-04 16:21:10,897 [ERROR] >     <DelayedCall 0x978549e0 [4.73240303993s] called=0 cancelled=0 LoopingCall<5.0>(ChannelCommunity._periodically_clean_delayed, *(), **{})()>
I: ERROR
I: ======================================================================
I: ERROR: test_add_torrent_from_url_to_channel_with_description (Tribler.Test.Core.Modules.RestApi.Channels.test_channels_torrents_endpoint.TestModifyChannelTorrentEndpoint)
I: ----------------------------------------------------------------------
I: Traceback (most recent call last):
I:   File "/data/data/org.tribler.android/files/lib/python2.7/site-packages/Tribler/Test/test_as_server.py", line 274, in tearDown
I:     super(TestAsServer, self).tearDown(annotate=False)
I:   File "/data/data/org.tribler.android/files/lib/python2.7/site-packages/Tribler/Core/Utilities/twisted_thread.py", line 151, in errback
I:     failure.raiseException()
I:   File "/data/data/org.tribler.android/files/lib/python2.7/site-packages/twisted/internet/defer.py", line 588, in _runCallbacks
I:     current.result = callback(current.result, *args, **kw)
I:   File "/data/data/org.tribler.android/files/lib/python2.7/site-packages/Tribler/Test/test_as_server.py", line 143, in checkReactor
I:     self.assertFalse(delayed_calls, "The reactor was dirty when tearing down the test")
I: AssertionError: The reactor was dirty when tearing down the test
I: ----------------------------------------------------------------------

# Fixed code
def check_new_version(self):
    def parse_body(body):
        if body is None:
            return
        version = json.loads(body)["name"][1:]
        if LooseVersion(version) > LooseVersion(version_id):
            self.session.notifier.notify(NTFY_NEW_VERSION, NTFY_INSERT, None, version)

    def on_request_error(failure):
        failure.trap(SchemeNotSupported, ConnectError, DNSLookupError)
        self._logger.error("Error when performing version check request: %s", failure)

    def on_response_error(failure):
        failure.trap(HttpError)
        self._logger.warning(
            "Got response code %s when performing version check request",
            failure.value.response.code,
        )

    deferred = http_get(VERSION_CHECK_URL)
    deferred.addErrback(on_response_error).addCallback(parse_body).addErrback(
        on_request_error
    )
    return deferred