Hi <#CP2HDHKE1|>!! I want to write a failure task ...
# flyte-support
w
Hi #CP2HDHKE1!! I want to write a failure task on workflow failure which print the error and tasks name (To send a slack notification), but I seeing that error field is set to None. Do you know what could be the issue? Code in the 🧵
Copy code
@task  # type: ignore[misc]
def current_time_task(name: str) -> str:
    """Dummy task which prints current time."""
    current_time = datetime.datetime.now(datetime.timezone.utc)
    task_output = f"Hello, {name}!  This is my task running at {current_time}"
    <http://logger.info|logger.info>(task_output)
    raise ValueError("Fail!")
    return task_output


@task
def failure_cb(name: str, err: FlyteError | None) -> str:
    print(f"Handling error: {err}")
    return name


@workflow(on_failure=failure_cb)  # type: ignore[misc]
def dummy_workflow(name: str) -> str:
    """Dummy workflow."""
    return current_time_task(name=name)  # type: ignore[no-any-return]
Output
Copy code
Handling error: None
b
I believe pipe "|" is not well handled in flyte, try Optional[FlyteError], with typing.Optional
w
Copy code
-def failure_cb(name: str, err: FlyteError | None) -> str:
+def failure_cb(name: str, err: typing.Optional[FlyteError] = None) -> str:  # noqa: UP007
     print(f"Handling error: {err}")
     return name
I changed to Optional, still same error.
@freezing-airport-6809: Do you know what could be the issue here? I see a UT for this feature: https://github.com/flyteorg/flytekit/blob/master/tests/flytekit/unit/core/test_workflows.py#L518.
a
@worried-iron-1001 sorry if this is a naive question but, what's the use case fro writing that task considering Flyte already can send Slack notifications on Failure? Is it because the content of the notification is not informative enough or?
w
@average-finland-92144: That's correct. Apart from sending informative notifications, we may want not to send the notification based on the certain error message.
f
@worried-iron-1001 we today do not support sending the error in the back. I think this is a small change, but at the moment not prioritized
btw, we are adding support for
webhook
notifications as a task
so you will be able to add a task to send notifications anywhere