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

eerror

v3.0.1

Published

An error that can easily combine with additional context data

Downloads

1,473

Readme

EError

An error that can easily combine with additional context data

npm node MIT License

Package size dependencies Status

Build Status semantic-release Coverage Status

NPM

Usecase

In clear js we do that:

function errored() {
  /// ...
  const error = new Error('Entity not found');
  error.status = 404;
  error.code = 'NOT_FOUND';
  error.query = { /* ... */ };
  throw error;
}

With this library we can do that:

import EError from 'eerror';

function errored() {
  /// ...
  throw new EError('Entity not found').combine({
    query,
    code: 'NOT_FOUND',
    status: 404,
  });
}

// or
function errored() {
  /// ...
  throw new EError().combine({
    message: 'Entity not found',
    code: 'NOT_FOUND',
    status: 404,
    query,
  });
}

For clear logs don't combine error with very huge object, like models from database. Just put the most important data, such as the entity id or error codes from external services.

Error wrapping

Use static method wrap to add data to existing errors:

async function tryAndCatch(user) {
  try {
    await user.foo();
  } catch(error) {
    // this call only apply params to error object
    throw EError.wrap(error, { userId: user.id });
  }
}

Also, EError object has method wrap, who assign all of properties of EError instance to error, but not name and stack.

async function tryAndCatch(user) {
  try {
    await user.foo();
  } catch(error) {
    // this call apply all of properties from EError instance to error object
    throw new EError().combine({ query }).wrap(error, { userId: user.id });
    // thrower error with 'query' property, userId and all of data from error object
    // stacktrace and name properties will be get from error object
  }
}

Prepared errors

You can create prepared error constructors:

const MyBuisnessError = EError.prepare({ message: 'Something wen wrong', code: 42 });

MyBuisnessError instanceof EError // true

async function errored() {
  if (moonPhase) {
    throw new MyBuisnessError({ moonPhase }); // error will contain correct stacktrace,
    // code = 42 and moonPhase value,
    // message will be equal 'Something wen wrong'
  }
}

You can create prepared error extends from custom class:

// Base class must be extends from EError or childs
const MyBuisnessError2 = EError.prepare(MyBuisnessError, { success: false });

MyBuisnessError instanceof EError // true
MyBuisnessError2 instanceof EError // true

async function errored() {
  if (moonPhase) {
    throw new MyBuisnessError({ moonPhase }); // error will contain correct stacktrace,
    // code = 42, moonPhase value, success = false
    // and message will be equal 'Something wen wrong'
  }
}

Also you can wrap errors from prepared error:

const MyBuisnessError = EError.prepare({ message: 'Something wen wrong', code: 42 });

async function tryAndCatch(user) {
  try {
    await user.foo();
  } catch(error) {
    // throwed error message will be 'Something wen wrong',
    // and contain code = 42 and userId
    // stack and other additional properties from error will be saved
    throw MyBuisnessError.wrap(error, { userId: user.id });
  }
}

Conflicts resolving

What about conflicts resolving? How don't loose data when we combine error who contain 'code' with options { code: ... }? Yes, save the data in the renamed property!

const error = new EError()
  .combine({ message: 'first' })
  .combine({ message: 'second' });
  .combine({ message: 'N + 1' });

error.message === 'N + 1';
error.message_old2 === 'second';
error.message_old1 === 'first';

Because it error, you don't interact with this dynamic named properties manual, but you will se replaced data in logs. If you want, you can filtred 'backup' properties in your logger layer by regexp /.+_old\d+/