I’m trying to integrate my Fauna database in an Express application that’s running inside an AWS Lambda function using Serverless HTTP. I’m calling various API endpoints and to avoid repetitive code, I have created a single error handler function using Express.
I’m calling all my APIs using Axios and thus, my error error handler is something like:
if (axios.isAxiosError(error) && error.response) {
// error came from my Axios call
} else {
some other error like a syntax error for example
}
Just like Axios has isAxiosError() method, is there a way for me to check if an error came from my Fauna query?
The documentation has many references of error handling like:
I’d like to add such a functionality to my error handler, so I don’t have to write the same code again and again. Can I assume that err.errors()[0].description is there to stay or like a “Fauna standard” (as in the de facto way of throwing errors in the JS Driver)? err.name and err.message is something that can be commonly used by other stuff, so I want something that’s “Fauna exclusive” and a sure shot way to determine that the error has been thrown by Fauna JS Driver.
Currently, the only examples available there are for JavaScript clients. We hope to add coverage for our other host languages soon.
The TL;DR: When a Fauna transaction encounters an error, it sets an appropriate HTTP response code, and provides a JSON response that describes the error(s) encountered.
Since a transaction may fail for multiple error conditions, the exception’s errors() function returns an array. All of the JavaScript code samples are aware that when an exception occurs, there is at least one error, so they use the 0 offset. And then the standard code and description fields are available.
When a query fails because of a specific FQL function call, there is a position field that provides a path array indicating which portion of the query caused the problem:
The Fauna drivers don’t send the source text of a query to the database, so there’s no line number indicator. But the position array does target the specific function involved.
To distinguish between Axios and Fauna errors, all Fauna-generated exceptions should be the FaunaException type (in JavaScript, at least).
While I’ll need the top portion of your answer in my actual error handling function, I’m still looking for the FaunaException that you mentioned in the end of your answer. I’m unable to find a reference to that.
I rather found this:
import {errors} from 'faunadb'
if (error instanceof errors.FaunaError) {
// error raised by Fauna
}
I fell into a rabbit hole in Fanua Driver folder inside the node_modules directory to end up discovering this. I couldn’t find a documentation reference to this, so I’m not sure if this is here to stay, but I would surely hope so.
If you can shed some light about the FaunaException type, that would be great!
That definition should stay intact, unless we overhaul the error handling in the JavaScript driver. To my knowledge, we have no plans to do that. If we do make such a change, we’d be using a new major version of the driver.