error-create

Abstraction for creating custom JavaScript errors

error-create

var MyCustomError = require('error-create')('MyCustomError');
 
try {
  throw new MyCustomError("My message", 'MY_CODE', { foo: 'bar' });
} catch (e) {
  console.log(instanceof Error);         // true 
  console.log(instanceof MyCustomError); // true 
  console.log(String(e));                  // 'MyCustomError: My message' 
  console.log(e.code);                     // 'MY_CODE' 
  console.log(e.foo);                      // 'bar' 
}
$ npm install error-create

To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack

Tests

$ npm test