Hey all! Simple question: if I raise an error with...
# prefect-community
d
Hey all! Simple question: if I raise an error within a task (i.e.,
ValueError
) is it possible to recover that traceback/error from a state handler? Right now I can say “this task failed and will be retrying in X minutes”, but not “this task failed for reason Y and will retry in X minutes”. Or can we pass keyword arguments to a state handler with this signature:
Copy code
# What I have now.
def notify_on_retry(task: Task, old_state: State, new_state: Retrying) -> State: ...

# Maybe what I want?
def notify_on_retry(task: Task, old_state: State, new_state: Retrying, message: str) -> State: ...

# Alternative?
def notify_on_retry(task: Task, old_state: State, new_state: Retrying, **kwargs) -> State: ...
k
Try retrieving it with
new_state.result
d
Ohhhhhh okay. I can test it out real quick, but do you know if
new_state.result
would be the
ValueError
itself?
k
I think it is, maybe as JSON. I can test in a bit too
🙌 1
Copy code
from prefect import Flow, task
import prefect
def handler(obj, old_state, new_state):
    if new_state.is_failed():
        <http://prefect.context.logger.info|prefect.context.logger.info>(new_state.result)

    return new_state

@task(state_handlers=[handler])
def abc():
    raise ValueError("MESSAGE HERE")

with Flow(",,") as flow:
    abc()

flow.run()
You can try logging
type(new_state.result)
also. and you can pull the exception message with
new_state.result.message
I think
🙌 1
a
and if you want to e.g. send yourself a Slack message with a full traceback, check this example @Danny Vilela https://discourse.prefect.io/t/how-can-i-send-automated-failure-notifications-is-there-[…]t-a-full-exception-traceback-in-a-failure-notification/115