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

alpha-errors

v0.4.0

Published

Simple helper to create error domains with error codes

Downloads

2,286

Readme

alpha-errors

CircleCI

Simple system that introduces useful "code" property to your errors and facilitates error management

Features:

  • Typescript support
  • Custom code generators
  • No dependencies
  • Custom error properties
  • Default error messages

Install

npm install alpha-errors

Usage


import {create} from 'alpha-errors';

const errorsDomain = create();

export const Errors = {
    // this creates "error descriptor" - a function that creates final error object
    NOT_FOUND: errorsDomain.create('Entity not found'),
    UNAUTHORIZED: errorsDomain.create('Unauthorized', 20), // custom error code
}

// create via "new"
const notFoundError = new Errors.NOT_FOUND()

notFoundError instanceof Error; // true
notFoundError.code; // 1
notFoundError.message; // Entity not found

// or call like a function
const unauthorizedError = Errors.UNAUTHORIZED('Please login first');

unauthorizedError.code; // 20
unauthorizedError.message; // Please login first

API

See jsdoc in *.d.ts files for detailed api. Below few example

Custom code generator

// built-in generators

import {generators} from 'alpha-errors';

const number = generators.incrementNumber();
number(); // 1
number(); // 2
number(); // 3

const customizedNumber = generators.incrementNumber(100, 10);
customizedNumber(); // 100
customizedNumber(); // 110
customizedNumber(); // 120

// Uses util.format for final code
const format = generators.formatCode('WOO_%d');
format(); // WOO_1
format(); // WOO_2
format(); // WOO_3

const customizedFormat = generators.formatCode('WOO_%d', 100, 10);
format(); // WOO_100
format(); // WOO_110
format(); // WOO_120

// custom formatter function
const customFunctionFormat = generators.formatCode((n) => (n+'').padStart(4, '0'))
format(); // 0001
format(); // 0002
format(); // 0003

Provide custom generator for errors domain creator.

import {ErrorsDomain, generators} from 'alpha-errors';

const errorsDomain = new ErrorsDomain({codeGenerator: generators.formatCode('WOO_%d')});
const error = errorsDomain.create()() // error.code === 'WOO_1'

Default messages

You can always override default message for event descriptor

const error1 = errorsDomain.create('Default message')(); // error1.message === 'Default message'
const error2 = errorsDomain.create('Default message')('New message'); // error1.message === 'New message'

Extra properties

You can inject extra properties to final error.


const errorDescriptor = errorsDomain.create('Default message', undefined, {foo: 'bar', bar: 'noo'});

const error1 = errorDescriptor();
// error1.foo === 'bar'
// error1.bar === 'noo'

const error2 = errorDescriptor(undefined, {foo: 'override bar', newProperty: 'test'});
// error2.foo === 'override bar'
// error2.bar === 'noo'
// error2.newProperty === 'test'

Customize error class


const domain = new ErrorsDomain({errorClass: CustomErrorClass});

const errorDescriptor1 = domain.create();
const errorDescriptor2 = domain.create({errorClass: AnotherCustomErrorClass});

errorDescriptor1() instanceof CustomErrorClass; // true
errorDescriptor2() instanceof AnotherCustomErrorClass; // true

Check whether error is the error we're looking for

Extra method is checks whether provided object is an object, have the same error code and is an instance of error class defined in descriptor.


const INVALID_USER = errorsDomain.create('Invalid user', '1', {errorClass: CustomErrorClass});

INVALID_USER.is(new INVALID_USER); // true
INVALID_USER.is(new Error('Some error')); // false - code doesn't match and it's not an instance of CustomErrorClass
INVALID_USER.is(new CustomErrorClass('Some error')); // false - code doesn't match

Changelog

0.3.0

  • Added ErrorsDomain.create static method
  • Added ErrorsDomain.prototype.createErrors for quick domain errors generation

0.2.0

  • added "is" method to error descriptors for easy error check

0.1.3

  • "formatCode" generator accepts custom formatter function

0.1.2

  • Error descriptor is now an interface
  • Added "code" property to error descriptor

0.1.1

  • Added ability to set errorClass per error descriptor
  • Error descriptor options as first argument for ErrorsDomain.prototype.create