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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@util.js/errors

v0.41.3

Published

JavaScript utility methods for errors

Downloads

11

Readme

@util.js/errors

JavaScript utility methods for errors

@util.js/errors is part of Util.js.

Errors API

Kind: global class
Access: public

errors.AsyncError

Kind: instance class of Errors
Access: public

new AsyncError()

An error that preserves the stack trace of the thread that calls an asynchronous function.

Use catch to use this class. Use this class directly for instanceof checks.

errors.RethrownError

Kind: instance class of Errors
Access: public

new RethrownError(error, [message])

A wrapper of another error used for rethrowing.

This error preserves information about the original error and its stack trace.

A Stack Overflow article inspired this class.

Throws:

  • TypeError if the given error is not defined

| Param | Type | Description | | --------- | ------------------- | ----------------------------------------- | | error | Error | The error to rethrow | | [message] | String | A human-readable description of the error |

Example

function throwATypeError() {
  throw new TypeError("Invalid Argument");
}

function rethrowTheTypeError() {
  try {
    throwATypeError();
  } catch (error) {
    throw new RethrownError(error, "Lorem Ipsum");
  }
}

try {
  rethrowTheTypeError();
} catch (error) {
  console.log(error.stack);
  // This outputs a stack trace like the following:
  //
  // RethrownError: Lorem Ipsum
  //     at rethrowTheTypeError (/root/utiljs/packages/utiljs-errors/test/ErrorsTest.js:16:15)
  // TypeError: Invalid Argument
  //     at throwATypeError (/root/utiljs/packages/utiljs-errors/test/ErrorsTest.js:10:13)
  //     at rethrowTheTypeError (/root/utiljs/packages/utiljs-errors/test/ErrorsTest.js:14:9)
  //     at Context.it (/root/utiljs/packages/utiljs-errors/test/ErrorsTest.js:20:7)
  //     ...
}

errors.catch(promise, [message]) ⇒ Promise

Wraps the given promise so that errors caught preserve the stack trace of the calling thread.

The example compares the stack trace of a rejected promise using this method and the stack trace of a rejected promise that does not use this method.

Kind: instance method of Errors
Returns: Promise - A promise that upon rejection wraps the error in an AsyncError and rethrows
Throws:

  • TypeError If the given promise is not an instance of Promise

Access: public

| Param | Type | Description | | --------- | -------------------- | ----------------------------------------- | | promise | Promise | The promise to wrap | | [message] | String | A human-readable description of the error |

Example

const errors = require("@util.js/errors");
function rejectAPromise() {
  return Promise.reject(new TypeError("Fail!"));
}

errors
  .catch(rejectAPromise())
  .catch((error) => console.log("\n\nWith Caller Stack Trace\n" + error.stack));
// With Caller Stack Trace
// AsyncError: Fail!
//     at promise.catch.error (/root/utiljs/packages/utiljs-errors/lib/Errors.js:28:13)
//     at process._tickCallback (internal/process/next_tick.js:68:7)
//     at Function.Module.runMain (internal/modules/cjs/loader.js:746:11)
//     at startup (internal/bootstrap/node.js:240:19)
//     at bootstrapNodeJSCore (internal/bootstrap/node.js:564:3)
//     at runExample0 (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:23:11)
//     at runExample (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:12:7)
//     at Object.<anonymous> (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:4:31)
//     at Module._compile (internal/modules/cjs/loader.js:702:30)
//     at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
//     at Module.load (internal/modules/cjs/loader.js:612:32)
//     at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
//     at Function.Module._load (internal/modules/cjs/loader.js:543:3)
//     at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
// TypeError: Fail!
//     at rejectAPromise (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:20:27)
//     at runExample0 (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:23:12)
//     at runExample (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:12:7)
//     at Object.<anonymous> (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:4:31)
//     at Module._compile (internal/modules/cjs/loader.js:702:30)
//     at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
//     at Module.load (internal/modules/cjs/loader.js:612:32)
//     at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
//     at Function.Module._load (internal/modules/cjs/loader.js:543:3)
//     at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)

rejectAPromise().catch((error) =>
  console.log("\n\nWithout Caller Stack Trace\n" + error.stack)
);
// Without Caller Stack Trace
// TypeError: Fail!
//     at rejectAPromise (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:20:27)
//     at runExample0 (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:53:3)
//     at runExample (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:12:7)
//     at Object.<anonymous> (/root/utiljs/packages/utiljs-errors/doc/README-examples.js:4:31)
//     at Module._compile (internal/modules/cjs/loader.js:702:30)
//     at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
//     at Module.load (internal/modules/cjs/loader.js:612:32)
//     at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
//     at Function.Module._load (internal/modules/cjs/loader.js:543:3)
//     at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)