Easily-extendable error class for use with ES6 classes that properly handles inheritance, stack traces, and error naming
npx @tessl/cli install tessl/npm-es6-error@4.1.0ES6 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.
npm install es6-errorimport ExtendableError from "es6-error";For CommonJS:
const ExtendableError = require("es6-error");For TypeScript:
import ExtendableError from "es6-error";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;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 stringInstance 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:
Error.captureStackTrace when available (Node.js)instanceof checks for Error, ExtendableError, and derived classesUsage 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); // trueError.captureStackTrace for optimal stack tracesutil.inherits inheritance pattern./typings/index.d.tsExtendableError instances behave exactly like standard JavaScript Error objects:
try/catch blocks