Named Queries error trapping

I’ve started using Named Queries and I LOVE them.
Is there a way to trap the errors, for example:
 Unique Key violated
 Parent Key not found
 Insufficient Privileges
 etc.
I’m using a try/except block like this:

try:
 runNamedQuery( ‘myQueryName’, {‘partNumber’:pn,‘lotNumber’:lot} )
except:
 logger.error(‘myQueryName failed…’)

but I would like to capture a meaningful message vs. “something went wrong.”

1 Like

Does this work for you?:

import sys try: results = system.db.runNamedQuery("queryName") except:Preformatted text logger.error('queryName failed' + sys.exc_info()[1])

sys.exc_info()[0] contains the type of the error and [1] contains the message afaik.

edit: Forgot the import statement at the top. Couldn’t format the indentation either…

1 Like

Yes, that’s exactly what I needed.
Had to add “import sys” at the beginning of my script and it works like a charm.
Thank You