I’m working with Fuana and Python (and Pipedream) for automating a tasks in one of my businesses. I see this error handling topic and the Javascript example at the very bottom of the post. There isn’t a python example, I’d love to see one to understand how to do something similar in python. Forgive me, my Javascript to python translation isn’t so good.
OK, maybe it is the early morning for me, and my not so strong python skills and I’m stumbling.
I’d like to build a generic exception that can catch any of the errors referenced and then format and print to the logs. The following is along the lines.
My first challenge is I’m getting a “catching classes that do not inherit from BaseException is not allowed” on just faunadb.errors. It is looking right now like I need to except each potential error with something like faunadb.errors.BadRequest.
Next, the information in ex, when using faunadb.errors.BadRequest isn’t a list or a dictionary, maybe a string? Meaning I need to do a bit more work to parse each part of it, true?
Boy I need to stay in python more to make this stick.
try:
client.query(some bad query)
except faunadb.errors as ex:
print(f'Exception in executing Fauna Query:')
print(f'error code: {ex.errors}')
print(f'error description: {ex.errors}')
print(f'error position: {ex.errors}')
print(f'error failures: {ex.errors}')
print(f'error cause: {ex.errors}')
else:
print("success")
I had trouble with this too, and it actually had nothing to do with the Fauna error handling in my testing. I was getting this message even with a very simple query without a try- except block. I was able to resolve this issue by upgrading my Python version to 3.10 (was 3.9 before).
So, you can build a generic exception with FaunaError and don’t have to except each error type.
ex.errors is a list, which contains an object of type ErrorData.
So you could get all the necessary information about the error by accessing ex.errors[0]
This is my working example
from faunadb import query as q
from faunadb.client import FaunaClient
from faunadb.errors import FaunaError
client = FaunaClient(secret="my-secret")
try:
op = client.query(q.select([ "name"], q.get(q.ref(q.collection("Books"), "358040335102771285"))))
print(op)
except FaunaError as ex:
print('Exception in executing Fauna Query:')
print('error code:', ex.errors[0].code)
print('error description:', ex.errors[0].description)
print('error position:', ex.errors[0].position)
print('error failures:',ex.errors[0].failures)
print('error cause:', ex.errors[0].cause)
else:
print("success")
Returns
Exception in executing Fauna Query:
error code: value not found
error description: Value not found at path [name].
error position: ['from']
error failures: None
error cause: None
@ldavuluri Thank you! Really, thank you for taking time. This morning I was banging my head a bit, I thought I was doing some of the things you have shown here, but clearly I wasn’t. For instance I was trying some of the ex.error[0] notation and felt I wasn’t getting any where in understanding it.
I see the environment I’m running this in is Python 3.9. I just took your example and adjusted a few items for my set up and the example ran as expected in that things are properly excepting and catching the errors.
Your willingness to respond here with the example I’m grateful for. I’ll keep learning, and hopefully pass on this knowledge to someone else I can help.