def generate_error(code, message, action):
    """
    Generates an error to report test failure.

    Args:
        code(int): error code to display.
        message(str): error string to display.
        action(:class:`niveristand.clientapi.ErrorAction`): action to perform.

    Returns:
        If action is Continue, returns the generated error.

    """
    from niveristand.clientapi._realtimesequencedefinitionapi.erroraction import ErrorAction
    from niveristand import errors
    from niveristand.library._tasks import get_scheduler
    assert isinstance(action, ErrorAction)
    error = errors.SequenceError(code, message, action)
    get_scheduler().get_task_for_curr_thread().error = error
    if action is ErrorAction.ContinueSequenceExecution:
        return error
    else:
        raise error
