0

I have a failure hook in which I would want to send a mail with the exception that has been raised. Is there any way to access it?

# PIPELINE
@failure_hook
def email_message_on_failure(context: HookContext):
    logging.exception(e)
    mail_errors = Mail(
        body=str(e)
    )
    mail_errors.send()
Javi Torre
  • 724
  • 8
  • 23
  • 1
    What is "it" you're trying to access? – Jeff Gruenbaum Feb 17 '22 at 20:06
  • I am a newbie to dagster, but I've been told that if an exception is raised during my process, it does what it's specified in the failure_hook part. I wanted to send the name and text of the exception as the body of a mail. – Javi Torre Feb 17 '22 at 20:09
  • 1
    is that not what's happening in the `mail_errors = Mail(body=str(e))` line? The body of the email is being set to the str representaiton of the exception `e`. – Jeff Gruenbaum Feb 17 '22 at 20:21
  • yeah, but that e is not being recognised – Javi Torre Feb 17 '22 at 20:22
  • Hmm, not sure how to help unless you are able to provide a traceback or show a way to replicate this. Looks like the code should be working. – Jeff Gruenbaum Feb 17 '22 at 20:23
  • This could be replicated by creating a solid which just contains a raise ValueError. Upon failure, the program is going to go into the failure_hook part, where that e that I want the error to be is not recognised. – Javi Torre Feb 17 '22 at 20:28

1 Answers1

0

The HookContext object accepted as a parameter to the failure hook has an op_exception property that allows you to reference the exception that occurred.

You can write something like:

@failure_hook
def email_message_on_failure(context: HookContext):
    e = context.op_exception
    ...