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

ebec

v2.3.0

Published

A library that provides a basic ES6 error class.

Downloads

374,198

Readme

ebec 🥋

npm version main codecov codecov

A library that simplifies error handling by providing an ES6 error class and utility functions. This library facilitates the extraction of options and error messages from constructor arguments.

Table of Contents

Installation

npm install ebec --save

Usage

The BaseError class accepts various constructor arguments of type Input and any Options specified during initialization are automatically assigned as attributes.

Simple

Create error instances in different ways, as demonstrated in the following examples:

Example #1

import { BaseError } from 'ebec';

const error = new BaseError('An error occurred.');

console.log(error.message);
// An error occurred.

Example #2

In this example, only error options are passed as a single argument to the error constructor.

import { BaseError } from 'ebec';

const error = new BaseError({
    message: 'The entity could not be found',
    code: 'BAD_REQUEST'
});

console.log(error.message);
// The entity could not be found

console.log(error.code);
// BAD_REQUEST

Example #3

In this example, multiple arguments are passed to the error constructor.

import { BaseError } from 'ebec';

const cause = new Error('foo');

const error = new BaseError(
    'The entity could not be found',
    {
        code: 'BAD_REQUEST'
    },
    cause
);

console.log(error.message);
// The entity could not be found

console.log(error.code);
// BAD_REQUEST

console.log(error.cause);
// { message: 'foo', ... }

Inheritance

Custom error classes that inherit from BaseError allow for more specific error handling.

import {
    BaseError, 
    Options
} from 'ebec';

export class NotFoundError extends BaseError {
    constructor(message?: string) {
        super({
            message,
            logMessage: true,
            logLevel: 'warning',
            code: 'NOT_FOUND'
        });
    }

}

Types

Input

type Input = Options | Error | string;

Options

type Options = {
    /**
     * The actual error message, if not provided on another way.
     */
    message?: string,

    /**
     * Trace of which functions were called.
     */
    stack?: string

    /**
     * A unique identifier for the error,
     * which can be a short uppercase string or a numeric code.
     */
    code?: string | number | null,

    /**
     * Additional data associated with the error. This property can hold
     * unstructured information or supplementary details that provide context
     * to the error.
     */
    data?: unknown,

    /**
     * Determines whether the error message can be safely exposed externally.
     */
    expose?: boolean;

    /**
     * Indicates whether the error should be logged in the application's logs.
     */
    logMessage?: boolean,

    /**
     * Specifies the log level at which this error should be recorded.
     */
    logLevel?: string | number,

    /**
     * Represents the underlying cause or source of the error.
     */
    cause?: unknown
};

Utils

isBaseError

This method is used to determine if the error is a basic error or if the error extends this class.

import { isBaseError, BaseError } from "ebec";

const error = new BaseError();

console.log(isBaseError(error));
// true

License

Made with 💚

Published under MIT License.