https://prefect.io logo
Title
c

Christian Juhl

09/27/2022, 3:34 PM
Hi guys, I am brand new to Prefect and trying to get familiar. I want my flow to finish in failed state but with a custom error message in the log and not the full exception detail:
from prefect import flow, task, get_run_logger
from prefect.orion.schemas.states import Failed
def my_function():
raise Exception('This is an exception')
@flow
def run_flow():
logger = get_run_logger()
try:
result = my_function()
<http://logger.info|logger.info>(result)
except Exception as e:
errmsg = f"Error! Type: {e.__class__.__name__} Detail: {e}"
logger.error(errmsg)
return Failed()
if __name__ == "__main__":
run_flow()
However, I get the following error; Exception has occurred: TypeError Unexpected result for failure state: None —— NoneType cannot be resolved into an exception File "main.py", line 21, in <module> run_flow() I'm thinking I might be doing more than one thing wrong here. Can anyone enlighten me, please? Thanks!
1
c

Christopher Boyd

09/27/2022, 4:01 PM
Hi Christian, The first thing that stands out to me, is that you are assigning:
result = my_function()
but
my_function()
has no return, which is presumably then
None
k

Khuyen Tran

09/27/2022, 4:02 PM
I tested your code and this line causes the error
return Failed()
c

Christian Juhl

09/28/2022, 2:01 PM
Thanks @Christopher Boyd, but I don't understand why I need the function to return anything. I just need it to throw an exception. The following code throws the same error:
from prefect import flow, task, get_run_logger
from prefect.orion.schemas.states import Failed
@task
def my_function():
return 1
@flow
def run_flow():
logger = get_run_logger()
try:
result = my_function()
<http://logger.info|logger.info>(result)
raise Exception('This is an exception')
except Exception as e:
errmsg = f"Error! Type: {e.__class__.__name__} Detail: {e}"
logger.error(errmsg)
return Failed()
if __name__ == "__main__":
run_flow()
Thanks @Khuyen Tran, I realize that returning the Failed state is causing the error, but I don't know how else I would get my flow to finish in Failed state.
c

Christopher Boyd

09/28/2022, 2:23 PM
Hi Christian, by default a flow fails with the summary state of all its tasks
So if that task failed , the flow should be marked as failed
c

Christian Juhl

10/04/2022, 3:25 PM
I see, but I still don't understand why the exception is thrown. Take this example from https://docs.prefect.io/concepts/flows/#return-a-manual-state If I run the code as is it finishes without errors, but if I change
if y.result() == "success":
to
if y.result() != "success":
it will throw the same exception: Exception has occurred: TypeError Unexpected result for failure state: None —— NoneType cannot be resolved into an exception
c

Christopher Boyd

10/04/2022, 4:04 PM
You are not returning a return from the exception, so it’s “catching” a none type I think
try adding some print logic around the assignment and return
this isn’t really a prefect error though, is it?
this seems like some generic type error in the code
It’s not a result of the return states