# Buggy code
def _check_version(self, distribution, current_version=None):
    response = requests.get("https://pypi.python.org/pypi/{}/json".format(distribution))
    try:
        data = response.json()
    except ValueError:
        return None
    if current_version is None:
        try:
            current_version = get_distribution(distribution).version
        except DistributionNotFound:
            return None
    current_version = Version(current_version)
    if current_version.is_prerelease:
        # if we are on a prerelease, get the latest one even if it's also a prerelease
        latest_version = Version(data["info"]["version"])
    else:
        # if we are stable, get the latest stable version
        versions = [v for v in map(Version, data["releases"]) if not v.is_prerelease]
        latest_version = max(versions) if versions else None
    return {
        "current_version": unicode(current_version),
        "latest_version": unicode(latest_version) if latest_version else None,
        "outdated": (current_version < latest_version) if latest_version else False,
    }


# Traceback (ConnectionError)
2018-01-18 06:57:43,036  28e04a15777a48cb  indico.flask - ERROR errors.py:107 -- HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url: /pypi/indico/json (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xda97a90>: Failed to establish a new connection: [Errno -2] Name or service not known',))

Traceback (most recent call last):
File "/opt/indico/.venv/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/indico/.venv/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/indico/.venv/lib/python2.7/site-packages/indico/web/flask/util.py", line 114, in wrapper
return obj().process()
File "/opt/indico/.venv/lib/python2.7/site-packages/indico/web/rh.py", line 270, in process
res = self._do_process()
File "/opt/indico/.venv/lib/python2.7/site-packages/indico/web/rh.py", line 249, in _do_process
return self._process()
File "/opt/indico/.venv/lib/python2.7/site-packages/indico/modules/core/controllers.py", line 205, in _process
return jsonify(indico=self._check_version('indico', indico.__version__),
File "/opt/indico/.venv/lib/python2.7/site-packages/indico/modules/core/controllers.py", line 182, in _check_version
response = requests.get('https://pypi.python.org/pypi/{}/json'.format(distribution))
File "/opt/indico/.venv/lib/python2.7/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/opt/indico/.venv/lib/python2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/opt/indico/.venv/lib/python2.7/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/opt/indico/.venv/lib/python2.7/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/opt/indico/.venv/lib/python2.7/site-packages/requests/adapters.py", line 508, in send
raise ConnectionError(e, request=request)
ConnectionError: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url: /pypi/indico/json (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0xda97a90>: Failed to establish a new connection: [Errno -2] Name or service not known',))

{u'data': {u'get': {'_': u'1516255062639'},
u'headers': {'Accept': u'*/*',
'Accept-Encoding': u'gzip, deflate, br',
'Accept-Language': u'en-US,en;q=0.5',
'Content-Length': u'',
'Content-Type': u'',
'Cookie': u'indico_session=***; ajs_user_id=null; ajs_group_id=null; ajs_anonymous_id=%2200000000000000000000000000%22',
'Host': u'indico.openbrain.sk',
'Referer': u'https://indico.openbrain.sk/admin/settings/',
'User-Agent': u'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0',
'X-Csrf-Token': u'***',
'X-Requested-With': u'XMLHttpRequest'},
u'json': None,
u'post': {},
u'url': {}},
u'endpoint': u'core.version_check',
u'id': '28e04a15777a48cb',
u'ip': '46.193.138.1',
u'method': 'GET',
u'referrer': 'https://indico.openbrain.sk/admin/settings/',
u'rh': 'RHVersionCheck',
u'time': '2018-01-18T06:57:43.111483',
u'url': u'https://indico.openbrain.sk/admin/version-check?_=1516255062639',
u'user': {u'email': u'mvala@saske.sk', u'id': 1, u'name': u'Martin Vala'},
u'user_agent': u'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0'}

# Fixed code
def _check_version(self, distribution, current_version=None):
    try:
        response = requests.get(
            "https://pypi.python.org/pypi/{}/json".format(distribution)
        )
    except requests.RequestException as exc:
        Logger.get("versioncheck").warning(
            "Version check for %s failed: %s", distribution, exc
        )
        raise NoReportError.wrap_exc(ServiceUnavailable())
    try:
        data = response.json()
    except ValueError:
        return None
    if current_version is None:
        try:
            current_version = get_distribution(distribution).version
        except DistributionNotFound:
            return None
    current_version = Version(current_version)
    if current_version.is_prerelease:
        # if we are on a prerelease, get the latest one even if it's also a prerelease
        latest_version = Version(data["info"]["version"])
    else:
        # if we are stable, get the latest stable version
        versions = [v for v in map(Version, data["releases"]) if not v.is_prerelease]
        latest_version = max(versions) if versions else None
    return {
        "current_version": unicode(current_version),
        "latest_version": unicode(latest_version) if latest_version else None,
        "outdated": (current_version < latest_version) if latest_version else False,
    }