CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-es6-error

Easily-extendable error class for use with ES6 classes that properly handles inheritance, stack traces, and error naming

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

ES6 Error

ES6 Error provides an easily-extendable Error class for use with ES6 classes that properly handles inheritance, stack traces, and error naming. It solves common issues with extending JavaScript's built-in Error class by automatically setting up proper stack trace capture, correctly propagating error messages, and ensuring the error name matches the constructor name for better debugging.

Package Information

  • Package Name: es6-error
  • Package Type: npm
  • Language: JavaScript (ES6+) with TypeScript definitions
  • Installation: npm install es6-error

Core Imports

import ExtendableError from "es6-error";

For CommonJS:

const ExtendableError = require("es6-error");

For TypeScript:

import ExtendableError from "es6-error";

Basic Usage

import ExtendableError from "es6-error";

// Create a custom error class
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);
  }
}

// Use the custom error
throw new MyError("Something went wrong");

// ES5 Usage
const util = require("util");
const ExtendableError = require("es6-error");

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

util.inherits(MyError, ExtendableError);

module.exports = MyError;

Capabilities

ExtendableError Class

An easily-extendable Error class that extends the built-in JavaScript Error with proper inheritance behavior.

/**
 * An easily-extendable Error class for use with ES6 classes
 * @param {string} [message=''] - Error message
 */
class ExtendableError extends Error {
  constructor(message = '');
}

Constructor Parameters:

  • message (string, optional): Error message, defaults to empty string

Instance Properties:

  • name (string): Automatically set to constructor name (non-enumerable, configurable, writable)
  • message (string): Error message (non-enumerable, configurable, writable)
  • stack (string): Stack trace (non-enumerable, configurable, writable)

Key Features:

  • Proper stack trace capture using Error.captureStackTrace when available (Node.js)
  • Fallback stack trace generation for browsers
  • Automatic name property setting based on constructor name
  • Cross-platform compatibility (Node.js and browsers)
  • Supports both ES6 class extension and ES5 inheritance patterns
  • All instances pass instanceof checks for Error, ExtendableError, and derived classes
  • Properties are correctly configured as non-enumerable for proper serialization

Usage Examples:

import ExtendableError from "es6-error";

// Basic custom error
class ValidationError extends ExtendableError {}

const error = new ValidationError("Invalid input");
console.log(error.name);        // "ValidationError"
console.log(error.message);     // "Invalid input"
console.log(error instanceof Error);            // true
console.log(error instanceof ExtendableError);  // true
console.log(error instanceof ValidationError);  // true

// Custom error with constructor
class HttpError extends ExtendableError {
  constructor(status, message) {
    super(message || `HTTP Error ${status}`);
    this.status = status;
  }
}

const httpError = new HttpError(404, "Not Found");
console.log(httpError.name);     // "HttpError"
console.log(httpError.message);  // "Not Found"
console.log(httpError.status);   // 404

// Multiple inheritance levels
class AuthError extends ValidationError {
  constructor(message) {
    super(message || "Authentication failed");
  }
}

const authError = new AuthError();
console.log(authError instanceof Error);           // true
console.log(authError instanceof ExtendableError); // true
console.log(authError instanceof ValidationError); // true
console.log(authError instanceof AuthError);       // true

Environment Compatibility

  • Node.js: Full support with Error.captureStackTrace for optimal stack traces
  • Browsers: Full support with fallback stack trace generation
  • ES6: Native class syntax support
  • ES5: Compatible with util.inherits inheritance pattern
  • TypeScript: Type definitions available at ./typings/index.d.ts

Error Handling

ExtendableError instances behave exactly like standard JavaScript Error objects:

  • Can be thrown and caught normally
  • Work with try/catch blocks
  • Support all standard Error methods (toString, etc.)
  • Maintain proper stack traces for debugging
  • Support error serialization and logging

Known Issues

  • Uglification can obscure error class names in production builds
  • Stack trace quality may vary between JavaScript engines

Install with Tessl CLI

npx tessl i tessl/npm-es6-error
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/es6-error@4.1.x
Publish Source
CLI
Badge
tessl/npm-es6-error badge