<@ULVA73B9P> I have automation in which my body is...
# ask-marvin
a
@Marvin I have automation in which my body is
Copy code
Flow run {{ flow.name }}/{{ flow_run.name }} observed in state `{{ flow_run.state.name }}` at {{ flow_run.state.timestamp }}.
State message: {{ flow_run.state.message }}
Deployment: {{ deployment.name }}

Flow run URL: {{ flow_run|ui_url }}
I want to add Flow error message instead of state message I am using prefect version 2.20
m
In Prefect 2.20, it seems there's no direct attribute specifically named
flow.error_message
. However, you can customize notifications or retrieve exceptions or error details by implementing state change hooks or logging within your flow or task functions. ### Step-by-Step Guide 1. Using State Change Hooks: You can use state change hooks to trigger actions (like sending notifications) when a flow or task enters a certain state (e.g.,
Failed
state). Here's an example from the documentation:
Copy code
python
   from prefect import flow
   from prefect.blocks.core import Block
   from prefect.settings import PREFECT_API_URL

   def notify_slack(flow, flow_run, state):
       # Example custom notification function
       if state.is_failed():
           slack_webhook_block = Block.load("slack-webhook/my-slack-webhook")
           slack_webhook_block.notify(
               f"Your flow {flow_run.name} failed with message:\n\n"
               f"See <https://{PREFECT_API_URL.value()}/flow-runs/flow-run/{flow_run.id}|details>"
           )
2. Capture Error Details in Tasks: You can add error handling within each task to log or collect error messages:
Copy code
python
   from prefect import task

   @task
   def my_task():
       try:
           # task logic that might fail
           pass
       except Exception as error:
           error_message = str(error)
           # process or log the error message
3. Access State Messages: Use the built-in state messages to obtain details from failed tasks or flows. You already have this in your automation configuration:
Copy code
text
   State message: {{ flow_run.state.message }}
4. GitHub Issues: It appears this approach aligns with the current Prefect implementation in version 2.20. You can look into ongoing discussions or feature requests on GitHub issues related to error handling in Prefect. By combining these strategies, you can manage and report errors effectively within Prefect 2.20. For more ideas, see the Prefect documentation on flows and error handling.