CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/npm-es6-error

Easily-extendable error for use with ES6 classes

Overview
Eval results
Files

ES6 Error

ES6 Error provides an easily-extendable Error class specifically designed for ES6 classes that addresses common issues with extending the built-in JavaScript Error object. It properly handles error message propagation, constructor name assignment, and stack trace capturing across different JavaScript environments.

Package Information

  • Package Name: es6-error
  • Package Type: npm
  • Language: JavaScript (with ES6 classes)
  • 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 ValidationError extends ExtendableError {
  constructor(message = "Validation failed") {
    super(message);
  }
}

// Use the custom error
try {
  throw new ValidationError("Username is required");
} catch (error) {
  console.log(error.name);       // "ValidationError"
  console.log(error.message);    // "Username is required"
  console.log(error.stack);      // Proper stack trace
  console.log(error instanceof Error);              // true
  console.log(error instanceof ExtendableError);    // true
  console.log(error instanceof ValidationError);    // true
}

Architecture

ES6 Error solves the common problems with extending JavaScript's built-in Error class:

  • Message Propagation: Ensures error messages are properly set and accessible
  • Constructor Name: Automatically sets the name property to the constructor name
  • Stack Trace: Properly captures stack traces using Error.captureStackTrace when available
  • Inheritance Chain: Maintains proper instanceof relationships across the inheritance hierarchy
  • Cross-Environment: Works in both Node.js and browser environments
  • ES5/ES6 Compatible: Supports both ES6 class syntax and traditional ES5 inheritance patterns

Capabilities

ExtendableError Class

Base error class that properly extends the native Error class with support for ES6 inheritance patterns.

/**
 * ExtendableError class that properly extends Error
 * Exported as default export from es6-error package
 * @param message - Error message, defaults to empty string
 */
export default class ExtendableError extends Error {
  constructor(message = '');
  
  /** Error message as provided to constructor (non-enumerable) */
  message: string;
  
  /** Constructor name, automatically set to class name (non-enumerable) */
  name: string;
  
  /** Stack trace, properly captured (non-enumerable) */
  stack: string;
  
  /** String representation of the error (returns the name) */
  toString(): string;
}

Usage Example:

import ExtendableError from "es6-error";

// Direct usage
const error = new ExtendableError("Something went wrong");
console.log(error.message); // "Something went wrong"
console.log(error.name);    // "ExtendableError"

// ES6 class inheritance
class CustomError extends ExtendableError {
  constructor(message = "Custom error occurred") {
    super(message);
  }
}

const customError = new CustomError();
console.log(customError.name); // "CustomError"
console.log(customError instanceof Error);             // true
console.log(customError instanceof ExtendableError);   // true
console.log(customError instanceof CustomError);       // true

Multi-level Inheritance

ExtendableError supports multiple levels of inheritance while maintaining proper type relationships.

import ExtendableError from "es6-error";

// Base application error
class AppError extends ExtendableError {}

// Specific error types
class ValidationError extends AppError {}
class NetworkError extends AppError {}

// Deep inheritance
class TimeoutError extends NetworkError {}

const error = new TimeoutError("Request timed out");
console.log(error instanceof Error);           // true
console.log(error instanceof ExtendableError); // true
console.log(error instanceof AppError);        // true
console.log(error instanceof NetworkError);    // true
console.log(error instanceof TimeoutError);    // true

ES5 Compatibility

For environments that don't support ES6 classes, use traditional inheritance patterns.

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;

Properties

message

/** Error message as provided to constructor (non-enumerable) */
message: string;

The error message is properly propagated and accessible, addressing the common issue where extending Error doesn't correctly set the message property. The property is configured as non-enumerable, writable, and configurable.

name

/** Constructor name, automatically set to class name (non-enumerable) */
name: string;

The name property is automatically set to the constructor name, ensuring proper error identification in logs and stack traces. The property is configured as non-enumerable, writable, and configurable.

stack

/** Stack trace, properly captured (non-enumerable) */
stack: string;

Stack traces are properly captured using Error.captureStackTrace when available (Node.js), or fallback to creating a new Error for the stack trace in browser environments. The property is configured as non-enumerable, writable, and configurable.

toString

/** String representation of the error (returns the name) */
toString(): string;

Returns the string representation of the error, which is the constructor name. This method is inherited from Error but properly returns the custom error class name.

Usage Example:

import ExtendableError from "es6-error";

class CustomError extends ExtendableError {}

const error = new CustomError("Something went wrong");
console.log(error.toString()); // "CustomError"
tessl i tessl/npm-es6-error@3.2.0
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/es6-error@3.2.x