Check if error is thrown by Fauna?


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:

.catch((err) => console.error(
  'Error: [%s] %s: %s',,

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)? 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.

There is a section on error handling in the documentation: Error handling :: Fauna Documentation

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.

That might look like this:

  errors: [
      code: 'unauthorized',
      description: 'Unauthorized'

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:

  errors: [
      position: [
      code: 'invalid ref',
      description: "Ref refers to undefined collection 'does_not_exist'"

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).

Thank you for the detailed post as always @ewan.

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!

Sorry, my bad. I wrote FaunaException when I meant to write FaunaError.

You can see the definition here: faunadb-js/errors.js at v4 · fauna/faunadb-js · GitHub

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.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.