Common Error Classes for Node.js

1.2.0 · abandoned · verified Sun Apr 19

The `common-errors` package provides a comprehensive suite of custom error classes for Node.js, designed to offer more specific error types than native JavaScript `Error` objects. Originally intended to mirror error types found in other modern languages, it includes classes like `AlreadyInUseError`, `ArgumentError`, `NotFoundError`, and `AuthenticationRequiredError`. Key features highlighted in its documentation include the ability to append stack traces from asynchronously generated errors, dynamically generate custom error classes, and map HTTP status codes to errors for web service integration. The current stable version is 1.2.0. However, the package has not seen active development or updates in many years, with its last release dating back to 2017. Consequently, it is no longer actively maintained.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the creation and catching of various common error types, custom error generation, and the utility for prepending stack traces from inner errors.

const errors = require('common-errors');

try {
  // Example 1: Argument Null Error
  function processUser(username) {
    if (!username) {
      throw new errors.ArgumentNullError('username');
    }
    console.log(`Processing user: ${username}`);
  }
  processUser(null);
} catch (error) {
  console.error('Caught ArgumentNullError:', error.name, error.message);
  console.error(error.stack);
}

try {
  // Example 2: Custom Error Generation
  const MyCustomError = errors.generateClass('MyCustomError', 'CustomErrorBase');
  throw new MyCustomError('Something went wrong in my custom component!');
} catch (error) {
  console.error('Caught MyCustomError:', error.name, error.message);
}

// Example 3: Error with inner error and stack prepending
function fetchData() {
  try {
    throw new Error('Original underlying database error');
  } catch (err) {
    // Prepend the current stack to the inner error's stack
    const connectionError = new errors.ConnectionError('Failed to connect to DB.', errors.prependCurrentStack(err));
    throw connectionError;
  }
}

try {
  fetchData();
} catch (error) {
  console.error('Caught ConnectionError with inner error:', error.name, error.message);
  console.error('Full stack:', error.stack);
}

view raw JSON →