npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@loxjs/errors

v1.2.5

Published

A module designed to create a standardized error handling system

Downloads

2

Readme

@loxjs/errors

@loxjs/errors is a Node.js module designed to create a standardized error handling system. It extends the native JavaScript Error object, providing additional properties such as internal and HTTP status codes, and integrates with Express.js for handling errors in web applications.

Features

  • Extends the native Error object with additional properties and methods.
  • Provides a unified way to handle different types of errors.
  • Allows for internationalization (i18n) of error messages.
  • Supports custom error details and data payloads.
  • Integrates with Express.js to handle errors in HTTP requests.
  • Can generate both detailed and simplified error JSON representations.

Installation

Install the package with npm:

npm install @loxjs/errors

Or with yarn:

yarn add @loxjs/errors

Usage

Basic Error Creation

const Errors = require('@loxjs/errors');

// Create a new error
const myError = new Errors('MyCustomError', {
  message: 'Something went wrong',
  httpCode: 400,
  detail: 'Detailed information about the error',
  data: { additional: 'data' }
});

console.log(myError.toJSON()); // Outputs the error as a JSON object

Express.js Middleware

const express = require('express');
const app = express();
const { expressError } = require('@loxjs/errors');

// Use the middleware in your Express application
app.use(expressError({
  isPROD: process.env.NODE_ENV === 'production', // Use true to hide detailed error info in production
  errHandler: (errJSON) => {
    // Optional: A function to handle the error, e.g. logging it
    console.error(errJSON);
  },
  i18n: (code) => {
    // Optional: A function for internationalization
    return code; // Replace with actual i18n implementation
  }
}));

app.get('/', (req, res) => {
  throw new Error('Oops!'); // This will be caught by the expressError middleware
});

app.listen(3000);

Error Wrapper

const { errorWrapper } = require('@loxjs/errors');

// Wrap native errors or custom error codes
const wrappedError = errorWrapper('CustomErrorCode', {
  message: 'Custom error message',
  httpCode: 500
});

console.log(wrappedError.toString()); // Outputs the error as a JSON string

Internationalization

const { setI18n } = require('@loxjs/errors');

// Set an internationalization function
setI18n((errCode) => {
  const i18nMessages = {
    'CustomErrorCode': 'Translated custom error message',
    // Add more error code translations here
  };
  return i18nMessages[errCode] || errCode;
});

API Reference

Errors Class

  • new Errors(code, options)
    • code (String): The error code.
    • options (Object): Additional options for the error.
      • internalCode (String): Internal error code for debugging.
      • httpCode (Number): HTTP status code associated with the error.
      • message (String): Human-readable description of the error.
      • fileName (String): Name of the file where the error occurred.
      • lineNumber (Number): Line number where the error occurred.
      • stack (String): Stack trace.
      • detail (Any): Additional details about the error.
      • data (Any): Additional data payload.

errorWrapper Function

  • errorWrapper(loxErrorCodeOrNativeErrorEntity, optionsOrErrorMessage)
    • Wraps a custom error code or native error entity into a standardized error object.
    • loxErrorCodeOrNativeErrorEntity (String|Error): The error code or native error object.
    • optionsOrErrorMessage (Object|String): Additional options for the error or a message string.

expressError Middleware

  • expressError(options)
    • options (Object): Configuration options for the middleware.
      • isPROD (Boolean): Whether to run in production mode, which affects error detail visibility.
      • errHandler (Function): A function to handle the error, such as logging.
      • i18n (Function): A function for internationalizing error messages.

Contributing

Contributions to @loxjs/errors are welcome! Please ensure that your contributions adhere to the following guidelines:

  • Write clear, readable, and maintainable code.
  • Follow existing coding styles and practices.
  • Write meaningful commit messages.
  • Update the documentation accordingly.

For more detailed information, please read the contributing guide.

Enjoy using @loxjs/errors!