Error strategy on JS SDK

Hi fauns,

I’m thinking about error handling on JavaScript SDK. For now, we only do something like this:

import { Client, query } from 'faunadb';

const client = new Client({ secret: 'secret' });

const { Select, Get, Match, Index } = query;

async function action(): Promise<void> {
  try {
    const request: any = await client.query(
      Select(
        ['data'],
        Get(Match(Index('papers_by_date'), 'date')))
      )

    // do something with `request`
  } catch (err) {
    // handle error
    // maybe unauthorized? maybe bad request? maybe bad query?
    // i don't know
  }
}

action();

This can be very tricky if you need to know exactlly what type of error you have. ¿It’s on the roadmap something like this?:

//...
type Fauna.Request = { ?? }

const { data, error }: Fauna.Request = await client.query(
  Select(
    ['data'],
    Get(Match(Index('papers_by_date'), 'date')))
  )

if (error) {
  switch (error) {
    case 'unauthorized':
      // do something with especific error type
      break;
    case 'bad_request':
      // do something with especific error type
      break;
    default:
      break;
   }
}

//...

Thanks,

Hey,

Great idea, much how Golang handles errors. It’s also something that is especially painful in async/await style, but your solution might actually make it harder when using promises. Maybe a simple wrapper such as this could get you the best of both worlds? Golang style error handling pattern in Javascript | 5 Error

Of course, the feature request remains valid, maybe one day we could provide a second .query in golang style :slight_smile: