def _append_if_error(errors, sentence_id, test, explanation, token=None):
    """
    If a test fails, append an error/warning dictionary to errors.

    Args:
        errors: a list of errors
        sentence_id: ID of the sentence this applies to
        test: boolean from a checked expression
        explanation: user-friendly string explaining the error found by the flag

    Returns: the value of `flag`
    """
    if not test:
        errors.append({"sentence_id": sentence_id, "explanation": explanation, "token": token})
    return test