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

stack-tools

v0.2.2

Published

Tools for printing and parsing errors and their stacks

Downloads

10

Readme

stack-tools

stack-tools provides utilites for printing and parsing errors and their stacks. It can help with a number of common tasks like printing causal chains of errors (i.e. errors that reference to other errors through an error.cause property). It can also help you truncate stack frames present due to error generation.

Usage

Here are some of the most common usages of stack tools:

When code in your application or a library throws errors with error.cause properties, you can use stack-tools to print them so that all information about the error is preserved.

import { printErrors } from 'stack-tools';

try {
  const baseError = new Error('base');
  const wrapperError = new Error('wrapper');
  baseError.cause = wrapperError;

  throw wrapperError;
} catch (e) {
  console.error(printErrors(error));
  // Error: wrapper
  //    at <anonymous>:5:28
  // Caused by: Error: base
  //    at <anonymous>:4:25
}

Sometimes it is useful to reuse some code for building errors, but that code tends to end up on the stack, where it makes it harder for the developer to spot where the program entered a throw code path. stack-tools can be used to omit some frames.

import { parseError, printError } from 'stack-tools';

function makeBeautifulError() {
  const err = new Error('Everything else has gone wrong');

  const parsed = parseError(err);
  parsed.stack = parsed.stack.slice(1);
  err.stack = printError(parsed);

  return err;
}

If error.cause is overkill for your needs, you can add some extra information to an error by replacing its message. This function ensures that error.stack is also updated appropriately.

import { replaceMessage } from 'stack-tools';

const id;
try {
  /* */
} catch(e) {
  replaceMessage(e, `${e.message}\nid: ${id}`)
}

API

Utilities that work with stacks from any environment are in the stack-tools module, otherwise known as the base module.

The following methods are provided:

  • replaceMessage(error, message) replaces error.message with message and also updates the text of error.stack. message may also be a message => newMessage callback.
  • isError(value) returns true if value is a native error instance.
  • getErrors(error) returns an array of causally chained errors, e.g. [error, error.cause, error.cause.cause],
  • parseError(error, ?options) returns an error AST node. For more info see the AST type definitions.
  • parseErrors(errors, ?options) returns an array of error AST nodes.
  • printError(error, ?options) returns `${printHeader(errror)}\n${error.stack}`
  • printErrors(error, ?options) returns `${printError(error)}\nCaused by: ${printErrors(error.cause)}`

The following options are provided:

  • options.frames (default: true) indicates whether frame data should be parsed or printed. For parse methods frames: false will cause error.frames to be undefined. For print methods frames will not be printed even if they are present in an argument AST.