Easily-extendable error for use with ES6 classes
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.
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 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
}ES6 Error solves the common problems with extending JavaScript's built-in Error class:
name property to the constructor nameError.captureStackTrace when availableinstanceof relationships across the inheritance hierarchyBase 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); // trueExtendableError 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); // trueFor 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;/** 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.
/** 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 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.
/** 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