Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FaunaError and it's sub-types eat the stack trace. #532

Closed
milyord opened this issue Sep 9, 2021 · 2 comments · Fixed by #536
Closed

FaunaError and it's sub-types eat the stack trace. #532

milyord opened this issue Sep 9, 2021 · 2 comments · Fixed by #536

Comments

@milyord
Copy link

milyord commented Sep 9, 2021

faunadb-js/src/errors.js

Lines 21 to 41 in 18d58e7

function FaunaError(name, message, description) {
Error.call(this)
/**
* Name of this exception.
* @type {string}
*/
this.name = name
/**
* Message for this exception.
* @type {string}
*/
this.message = message
/**
* Description for this exception.
* @type {string}
*/
this.description = description
}

Extending the Error object is not ideal in JavaScript. You're better-off just decorating instances of Error like this:

function FaunaError(error) {
  error.name = "FaunaError";
  error.message = `Some Decoration - ${error.message}`;
  Object.defineProperty(error, "type", {value: "FaunaError"});
  return error;
}

And then when you need to use it:

// instanceof is a liar. Use error.type or invent your own API and stick to it.
throw FaunaError(Error("JavaScript !== Python && JavaScript !== Java && JavaScript !== ..."));

Benefits of this approach:

  1. Preserves the stack trace and all other desirable properties of an error.
  2. No need to use the new keyword.
  3. Simplifies your codebase and gets rid of all sub-typing boilerplate code e.g. _utils/inherits.

There might be downsides to this approach but I doubt any would be worse than loosing the stack trace.

Regardless of whether you like this approach or not FaunaError eats the stack trace and that's a bug.

@fireridlle fireridlle linked a pull request Sep 10, 2021 that will close this issue
@fireridlle
Copy link
Contributor

Extends custom errors from the Error class considered as a best practice.
Stack trace issue fixed and will be released soon

@faunaee
Copy link
Contributor

faunaee commented Jan 29, 2022

@milyord Just curious, but how would the stack trace from a Fauna Error help you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants