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

yerror

v8.0.0

Published

It helps to know why you got an error.

Downloads

53,463

Readme

yerror

It helps to know why you got an error.

GitHub license Coverage Status

Usage

First, require me where you could throw errors:

import YError from 'yerror';

Then, emit errors with a bonus: parameters!

function doSomething(pay, action) {
  if(parseInt(pay, 10) !== pay) {
    throw new YError('E_BAD_PAY', pay, action);
  }
}

doSomething('nuts', 'code');


// YError: E_BAD_PAY (nuts, code)
//   at doSomething (/home/nfroidure/nfroidure/yerror/test.js:5:11)
//   at Object.<anonymous> (/home/nfroidure/nfroidure/yerror/test.js:9:1)
//   (...)

You don't have to use constant like error messages, we use this convention mainly for i18n reasons.

Also, you could want to wrap errors and keep a valuable stack trace:

function doSomethingAsync(pay, action) {
  return  new Promise(function(resolve, reject) {
    try {
      doSomething(pay, action);
      resolve();
    } catch(err) {
      reject(YError.bump(err));
    }
  });
}

doSomethingAsync('nuts', 'code')
  .catch(function(err) {
    console.log(err.stack);
  });

// YError: E_BAD_PAY (nuts, code)
//    at doSomething (/home/nfroidure/nfroidure/yerror/test.js:5:11)
//    (...)
// YError: E_BAD_TRANSACTION (pay)
//    at Function.YError.wrap (/home/nfroidure/nfroidure/yerror/src/index.js:41:12)
//    at /home/nfroidure/nfroidure/yerror/test.js:16:21
//    at doSomethingAsync (/home/nfroidure/nfroidure/yerror/test.js:11:11)
//    (...)

API

Classes

Functions

YError ⇐ Error

A YError class able to contain some params and print better stack traces

Kind: global class
Extends: Error

YError.wrap(err, [errorCode], [...params]) ⇒ YError

Wraps any error and output a YError with an error code and some params as debug values.

Kind: static method of YError
Returns: YError - The wrapped error

| Param | Type | Default | Description | | --- | --- | --- | --- | | err | Error | | The error to wrap | | [errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error | | [...params] | YErrorParams | | Some additional debugging values |

YError.cast(err, [errorCode], [...params]) ⇒ YError

Return a YError as is or wraps any other error and output a YError with a code and some params as debug values.

Kind: static method of YError
Returns: YError - The wrapped error

| Param | Type | Default | Description | | --- | --- | --- | --- | | err | Error | | The error to cast | | [errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error | | [...params] | YErrorParams | | Some additional debugging values |

YError.bump(err, [errorCode], [...params]) ⇒ YError

Same than YError.wrap() but preserves the code and the debug values of the error if it is already an instance of the YError constructor.

Kind: static method of YError
Returns: YError - The wrapped error

| Param | Type | Default | Description | | --- | --- | --- | --- | | err | Error | | The error to bump | | [errorCode] | string | "'E_UNEXPECTED'" | The error code corresponding to the actual error | | [...params] | YErrorParams | | Some additional debugging values |

printStackTrace(err) ⇒ string

Allow to print a stack from anything (especially catched errors that may or may not contain errors 🤷).

Kind: global function
Returns: string - The stack trace if any

| Param | Type | Description | | --- | --- | --- | | err | Error | The error to print |

Authors

License

MIT