# Buggy code
def _on_loaded(response):
    if response.startswith(b"magnet"):
        _, infohash, _ = parse_magnetlink(response)
        if infohash:
            self.session.lm.ltmgr.get_metainfo(infohash, timeout=20).addCallback(
                on_got_metainfo
            )
            return

    # Otherwise, we directly invoke the on_got_metainfo method
    on_got_metainfo(bdecode(response))


# Traceback (exceptions.RuntimeError)
Unhandled error in Deferred:

Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/twisted/web/_newclient.py", line 1229, in _bodyDataFinished_CONNECTED
self._bodyProtocol.connectionLost(reason)
File "/Library/Python/2.7/site-packages/twisted/web/client.py", line 2170, in connectionLost
self.deferred.callback(b''.join(self.dataBuffer))
File "/Library/Python/2.7/site-packages/twisted/internet/defer.py", line 460, in callback
self._startRunCallbacks(result)
File "/Library/Python/2.7/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
self._runCallbacks()
--- <exception caught here> ---
File "/Library/Python/2.7/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/martijndevos/Documents/tribler/Tribler/Core/Modules/restapi/torrentinfo_endpoint.py", line 97, in on_lookup_error
failure.trap(ConnectError, DNSLookupError, HttpError, ConnectionLost)
File "/Library/Python/2.7/site-packages/twisted/python/failure.py", line 441, in trap
raise self
File "/Library/Python/2.7/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/martijndevos/Documents/tribler/Tribler/Core/Modules/restapi/torrentinfo_endpoint.py", line 110, in _on_loaded
on_got_metainfo(bdecode(response))
exceptions.RuntimeError: expected value (list, dict, int or string) in bencoded string

ERROR   1565616789.85            tribler_window:73   (root)  Traceback (most recent call last):
File "/Users/martijndevos/Documents/tribler/TriblerGUI/event_request_manager.py", line 114, in on_read_data
raise RuntimeError(json_dict["event"]["text"])
RuntimeError: [Failure instance: Traceback: <type 'exceptions.RuntimeError'>: expected value (list, dict, int or string) in bencoded string
/Library/Python/2.7/site-packages/twisted/web/_newclient.py:1229:_bodyDataFinished_CONNECTED
/Library/Python/2.7/site-packages/twisted/web/client.py:2170:connectionLost
/Library/Python/2.7/site-packages/twisted/internet/defer.py:460:callback
/Library/Python/2.7/site-packages/twisted/internet/defer.py:568:_startRunCallbacks
--- <exception caught here> ---
/Library/Python/2.7/site-packages/twisted/internet/defer.py:654:_runCallbacks
/Users/martijndevos/Documents/tribler/Tribler/Core/Modules/restapi/torrentinfo_endpoint.py:97:on_lookup_error
/Library/Python/2.7/site-packages/twisted/python/failure.py:441:trap
/Library/Python/2.7/site-packages/twisted/internet/defer.py:654:_runCallbacks
/Users/martijndevos/Documents/tribler/Tribler/Core/Modules/restapi/torrentinfo_endpoint.py:110:_on_loaded

# Fixed code
def _on_loaded(response):
    if response.startswith(b"magnet"):
        _, infohash, _ = parse_magnetlink(response)
        if infohash:
            self.session.lm.ltmgr.get_metainfo(infohash, timeout=20).addCallback(
                on_got_metainfo
            )
            return

    # Otherwise, we directly invoke the on_got_metainfo method
    try:
        decoded_response = bdecode(response)
        on_got_metainfo(decoded_response)
    except RuntimeError:
        # The decoding failed - handle it like a None metainfo
        on_got_metainfo(None)