def get_api_failure_reason(response):
    """
    Handy function to safely get a failure reason from
    the "detail" field of request.response object returned
    from our API
    """

    try:
        reason = response.json()["detail"]
    except (ValueError, KeyError):
        reason = "unknown reason"
    return reason