yourls/data.py
def _validate_yourls_response(response, data):
    """Validate response from YOURLS server."""
    try:
        response.raise_for_status()
    except HTTPError as http_exc:
        # Collect full HTTPError information so we can reraise later if required.
        http_error_info = sys.exc_info()

        # We will reraise outside of try..except block to prevent exception
        # chaining showing wrong traceback when we try and parse JSON response.
        reraise = False

        try:
            jsondata = response.json()
        except ValueError:
            reraise = True
        else:
            logger.debug('Received error {response} with JSON {json}',
                         response=response, json=jsondata)
            _handle_api_error_with_json(http_exc, jsondata, response)

        if reraise:
            six.reraise(*http_error_info)
    else:
        # We have a valid HTTP response, but we need to check what the API says
        # about the request.
        jsondata = response.json()

        logger.debug('Received {response} with JSON {json}', response=response,
                     json=jsondata)

        if {'status', 'code', 'message'} <= set(jsondata.keys()):
            status = jsondata['status']
            code = jsondata['code']
            message = jsondata['message']

            if status == 'fail':
                if code == 'error:keyword':
                    raise YOURLSKeywordExistsError(message, keyword=data['keyword'])
                elif code == 'error:url':
                    url = _json_to_shortened_url(jsondata['url'], jsondata['shorturl'])
                    raise YOURLSURLExistsError(message, url=url)
                else:
                    raise YOURLSAPIError(message)
            else:
                return jsondata
        else:
            # Without status, nothing special needs to be handled.
            return jsondata
def _validate_yourls_response(response, data):
    """Validate response from YOURLS server."""
    try:
        response.raise_for_status()
    except HTTPError as http_exc:
        # Collect full HTTPError information so we can reraise later if required.
        http_error_info = sys.exc_info()

        # We will reraise outside of try..except block to prevent exception
        # chaining showing wrong traceback when we try and parse JSON response.
        reraise = False

        try:
            jsondata = response.json()
        except ValueError:
            reraise = True
        else:
            logger.debug('Received error {response} with JSON {json}',
                         response=response, json=jsondata)
            _handle_api_error_with_json(http_exc, jsondata, response)

        if reraise:
            six.reraise(*http_error_info)
    else:
        # We have a valid HTTP response, but we need to check what the API says
        # about the request.
        jsondata = response.json()

        logger.debug('Received {response} with JSON {json}', response=response,
                     json=jsondata)

        if {'status', 'code', 'message'} <= set(jsondata.keys()):
            status = jsondata['status']
            code = jsondata['code']
            message = jsondata['message']

            if status == 'fail':
                if code == 'error:keyword':
                    raise YOURLSKeywordExistsError(message, keyword=data['keyword'])
                elif code == 'error:url':
                    url = _json_to_shortened_url(jsondata['url'], jsondata['shorturl'])
                    raise YOURLSURLExistsError(message, url=url)
                else:
                    raise YOURLSAPIError(message)
            else:
                return jsondata
        else:
            # Without status, nothing special needs to be handled.
            return jsondata
Validate response from YOURLS server.