Giter Site home page Giter Site logo

Comments (2)

bjyoungblood avatar bjyoungblood commented on June 11, 2024 5

The short answer

I hadn't really considered this. I originally built this library with the only intention of using it in Bluebird's .catch predicate filters (e.g. .catch(NotFoundError, (err) => { ... })).

This isn't ideal, but if you're using uglifyjs2, there are a few options available that might solve this for you quickly:

  • keep_fnames / --keep-fnames disables mangling of function names (easier, but obviously results in less minification)
  • reserved allows you to provide a list of identifiers that should not be mangled (harder to maintain)

The long answer

I've given a some thought to this library lately. I consider it to be a hack, and I'm not really a fan of hacks. Given the number of ways that it can break or fail to work as intended (uglifying, use with typescript, issues with different module bundlers, cross-environment issues, changes to babel, etc.), I would almost recommend not using it.

A much simpler pattern that involves zero hacks could look something like this:

const notFound = 'NotFoundError';

function notFoundError(err) {
  err.name = notFound;
  return err;
}

function isNotFoundError(err) {
  return err.name === notFound;
}

try {
  throw notFoundError(new Error('message'));
} catch(err) {
  if (isNotFoundError(err)) {
    // handle
  } else {
    // handle other
  }
}

This should still capture the correct stack trace and message, while preserving the error name. You could also create other decorator functions to add additional info/properties to the error.

from es6-error.

bjyoungblood avatar bjyoungblood commented on June 11, 2024

Added a link to this issue in the readme. Closing for now.

from es6-error.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.