Giter Site home page Giter Site logo

es6-error's Introduction

es6-error

npm version Build Status

An easily-extendable error class for use with ES6 classes (or ES5, if you so choose).

Tested in Node 4.0, Chrome, and Firefox.

Why?

I made this because I wanted to be able to extend Error for inheritance and type checking, but can never remember to add Error.captureStackTrace(this, this.constructor.name) to the constructor or how to get the proper name to print from console.log.

ES6 Usage

import ExtendableError from 'es6-error';

class MyError extends ExtendableError {
  // constructor is optional; you should omit it if you just want a custom error
  // type for inheritance and type checking
  constructor(message = 'Default message') {
    super(message);
  }
}

export default MyError;

ES5 Usage

var util = require('util');
var ExtendableError = require('es6-error');

function MyError(message) {
  message = message || 'Default message';
  ExtendableError.call(this, message);
}

util.inherits(MyError, ExtendableError);

module.exports = MyError;

Known Issues

  • Uglification can obscure error class names (#31)

Todo

  • Better browser compatibility
  • Browser tests

es6-error's People

Contributors

bjyoungblood avatar cganterh avatar chaintng avatar delta62 avatar existentialism avatar imurchie avatar krolow avatar lkay avatar reggi avatar tyscorp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

es6-error's Issues

jsnext:main should not point to completely untranspiled code

From the Rollup docs:

jsnext:main will point to a module that has ES2015 module syntax but otherwise only syntax features that node supports.

Libraries that expose an ES6 module build should transpile the code in a way that makes it ES5-compatible, but leaves "import" and "export" statements intact. The reason is that we can't transpile all node_modules for a project, nor would we know what babel config to use on each of them. See this conversation for more discussion. Pointing "jsnext:main" to a file with the "class" keyword is breaking our build.

jsnext:main usage

See #22 (and #23) for background.

I'm not currently using Rollup myself, so I'm not 100% sure how jsnext:main is supposed to work.

For a single-file module, it seems like the ES module syntax won't really provide any benefit (no need for tree-shaking as there is no tree to shake). If we just removed jsnext:main, would Rollup work properly?

Alternatively, it sounds like we could create a second build that does not use the add-module-exports Babel plugin and point jsnext:main to that.

babel config causes error with React Native

Having the "babel" key in package.json seems to throw an error when required from React Native.

The specific error is:

ReferenceError: Unknown plugin "add-module-exports" specified in "node_modules/es6-error/package.json" at 0, attempted to resolve relative to "node_modules/es6-error"

Fixing this should be as simple as moving the config to a .babelrc file.

dist was not built on last publish

latest publish does not contain fixes to err.message inside the dist folder.
i have babel disabled for node_modules so this bugs me :)

Edit: babel disabled does not matter since package.json points at dist/index.js

pkg.main, pkg.module are incompatible, so require("es6-error") behaves differently under Rollup/Webpack

In Node, require("es6-error") returns the function ExtendableError, but in Rollup or Webpack, require("es6-error") returns a module { default: ExtendableError }. This means an es6-error dependent can’t be compatible with both environments without contortions.

$ echo 'console.log(require("es6-error"))' > src.js
$ npm i quickselect webpack webpack-cli
$ node src.js
[Function: ExtendableError]
$ npx rollup src.js -p node-resolve -p commonjs -f cjs -o out.js
$ node out.js
[Object: null prototype] { default: [Function: ExtendableError] }
$ npx webpack -d
$ node dist/main.js 
Object [Module] { default: [Function: ExtendableError] }

This is because Node is using pkg.main (./lib/index), while Rollup and Webpack are using pkg.module (./es6/index.js), and the two files do not provide compatible interfaces.

Three potential solutions are:

  • disable babel-plugin-add-module-exports, so require("es6-error").default would work everywhere; or
  • add ExtendableError.default = ExtendableError for compatibility; or
  • remove pkg.module.

TypeError: Cannot read property 'prototype' of undefined"," at __extends

Hi

Getting above error when using with Typescript 2.1. This is where the error occurs on ES5 code generated under b.prototype:

var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); };

Here's is how i'm calling the module:

import ExtendableError from "es6-error";

export class AuthenticationError extends ExtendableError {}`

The ES5 version works:

`var util = require('util');
var ExtendableError = require('es6-error');

function AuthenticationError(message) {
message = message || 'Default message';
ExtendableError.call(this, message);
}

util.inherits(AuthenticationError, ExtendableError);

export {AuthenticationError}`

What am i doing wrong?

Curious about choices

I'm comparing your implementation to this one: https://gist.github.com/daliwali/09ca19032ab192524dc6

For instance:

  • Error.call(this, message) vs super(message)
  • Object.defineProperty(this, 'name' ... vs get name()
  • Lack of if (Error.hasOwnProperty('captureStackTrace'))
  • Lack of fallback to Object.defineProperty(this, 'stack', {

I see in your commit history that you had some of this and then changed it. I'd love to learn what problems you had or reasons for the changes.

Thanks!

Consider deprecating this package

I don't like to rain on other people's parades, but the examples in the README.md can be rewritten rather more simply as:

ES6 Usage

class MyError extends Error {}
MyError.prototype.name = 'MyError';
Object.defineProperty(MyError.prototype, 'name', {enumerable : false});  // Optional.

ES5 Usage

function MyError(message) {
  return Object.setPrototypeOf(new Error(message), MyError.prototype);
}
Object.setPrototypeOf(MyError.prototype, Error.prototype);
MyError.prototype.name = 'MyError';
Object.defineProperty(MyError.prototype, 'name', {enumerable: false});  // Optional.

These are both (AFAICT) exactly equivalent, and have the following advantages:

  • One fewer external dependency.
  • Shorter and simpler code.
  • The .name goes on the .prototype, instead of every instance. This saves memory and is more consistent with existing Error classes.
  • No .message property is created if no message is supplied to the constructor. This is more consistent with existing Error classes—and of course if you do want a default message, then it's easily done via the constructor as in the existing example.
  • Does not depend on the node.js-specific Error.captureStackTrace function.

`.message` is not defined

When used with babel, e.message returns nothing.

➜ ~ % node -v
v4.0.0
import ExError from 'es6-error'

export default class UserNotFound extends ExError {
}

try {
  throw new UserNotFound(':)')
} catch (e) {
  console.log(e.message)
  console.log(typeof e.message)
  console.log(e.message.length)
}

➡️


string
0

Does not work in combination with longjohn

When used in combination with https://github.com/mattinsler/longjohn, produces no stack trace.

Extending Error object directly:

/Users/gajus/Documents/dev/gajus/mightyql/dist/index.js:86
    throw new _errors.NotFoundError();
          ^
Error: Resource not found.
    at exports.many (/Users/gajus/Documents/dev/gajus/mightyql/dist/index.js:86:11)
---------------------------------------------
    at Object.<anonymous> (/Users/gajus/Documents/dev/applaudience/showtime-api/src/bin/server.js:42:9)
    at Module._compile (module.js:571:32)
    at loader (/Users/gajus/Documents/dev/applaudience/showtime-api/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/gajus/Documents/dev/applaudience/showtime-api/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Function.Module.runMain (module.js:605:10)
    at Object.<anonymous> (/Users/gajus/Documents/dev/applaudience/showtime-api/node_modules/babel-cli/lib/_babel-node.js:154:22)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)

Extending es6-error:

/Users/gajus/Documents/dev/gajus/mightyql/dist/index.js:86
    throw new _errors.NotFoundError();
          ^
NotFoundError: Resource not found.
    at exports.many (/Users/gajus/Documents/dev/gajus/mightyql/dist/index.js:86:11)

Integration tests

Currently, unit tests only target the un-transpiled ES6 source. This is fine for testing intended behavior, but it doesn't do anything to ensure the compiled output will work (in Node or in a browser).

It would be great to run the test suite against es6/index.js and lib/index.js after they are compiled.

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.