Surface errors more

Because it is not recommend to rely on the Error.message, it would be nice if errors were less nested as it’s a pain to get to them at the moment.

I’d like to second this feature request. It’s very difficult to tell which Abort was triggered in the query.

My current approach (and please tell me if I’m doing this wrong), is to create a FaunaError class and if the error is an instance of BadRequest I overwrite the code to the description of the Abort. This allows me to use the Abort() message for something useful. It feels like there could be a better way to know what the error is though.

Here is my error class if you’re interested.

export class FaunaError extends Error {
  code: string;
  statusCode: number;
  message: string;

  constructor(error: InstanceType<typeof FaunaHTTPError>) {
    super();

    // @ts-ignore
    const errors = error.requestResult.responseContent.errors;

    this.code = errors[0].code;
    this.message = errors[0].description;
    this.statusCode = 500;

    if (error instanceof BadRequest) {
      this.statusCode = 400;
      this.code = errors[0].cause[0].description;
    }

    if (error instanceof Unauthorized) {
      this.statusCode = 401;
    }

    if (error instanceof PermissionDenied) {
      this.statusCode = 403;
    }

    if (error instanceof NotFound) {
      this.statusCode = 404;
    }

    if (error instanceof MethodNotAllowed) {
      this.statusCode = 405;
    }

    if (error instanceof InvalidValue) {
      this.statusCode = 422;
    }

    if (error instanceof InternalError) {
      this.statusCode = 500;
    }

    if (error instanceof UnavailableError) {
      this.statusCode = 503;
    }
  }
}

And here is how I use it:

try {
  await faunaQuery();
  await somethingElse();
} catch(error) {
  if (error instanceof FaunaHTTPError) {
    const faunaError = new FaunaError(error);
    if (faunaError.code === 'invalid_credentials') {
      return res.status(400).send('Incorrect username or password.');
    }
  }
}

We’re happy to share that we’ve revamped error handling in FQL in our latest version of the language. Check out the details here