def funded_by_grant(paper, grant_id):
    """
    Determine whether the paper was funded by the specified grant,
    and if specified, the rationale behind how the paper aligns with the grant.

    Parameters
    ----------
    paper : dict
        The 'paper' entry from the papers.yaml database.
    grant_id : str
        The grant id from grants.yaml to query for

    Returns
    -------
    rationale : bool or str
        If a rationale is provided, a str containing the rationale of how the paper aligns with the grant is provided.
        If no rationale is provided, True is returned if the paper contains the grant_id as a funding source, False otherwise.

    """
    try:
        # get list of grants
        for grant in paper['funding']:
            # key: value entries may have a rationale
            if type(grant) is dict:
                if grant['id'] == grant_id:
                    if 'rationale' in grant:
                        # Return rationale if provided
                        return grant['rationale']
                    else:
                        return True
            # If we haven't specified a dict, there can be no rationale
            elif type(grant) is str:
                if grant == grant_id:
                    return True
    except Exception as e:
        pass

    return False