import requests


def handle_response(r, http_method, custom_err):
    """
    Handles the HTTP response and returns the JSON

    Parameters
    ----------
    r: requests module's response
    
    http_method: string
        "GET", "POST", "PUT", etc.

    custom_err: string
        the custom error message if any

    Returns
    -------
    json : dict

    """
    json = {}
    if r.status_code == requests.codes.ok:
        if r.text:
            json = r.json()
        else:
            print("{0} returned an empty response.".format(http_method))
    else:
        if custom_err is not None:
            print(custom_err)
        print("Status code: " + str(r.status_code))
        if r.text:
            print(r.text)
        r.raise_for_status()
    return json