import six


def body_to_json(response):
    """
    Returns the JSON-encoded contents of `response`, raising a detailed error on failure.

    Parameters
    ----------
    response : :class:`requests.Response`
        HTTP response.

    Returns
    -------
    contents : dict
        JSON-encoded contents of `response`.

    Raises
    ------
    ValueError
        If `response`'s contents are not JSON-encoded.

    """
    try:
        return response.json()
    except ValueError:  # not JSON response
        msg = '\n'.join([
            "expected JSON response from {}, but instead got:".format(response.url),
            response.text or "<empty response>",
            "",
            "Please notify the Verta development team.",
        ])
        msg = six.ensure_str(msg)
        six.raise_from(ValueError(msg), None)