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 🙏

© 2026 – Pkg Stats / Ryan Hefner

hierarchical-error

v1.0.1

Published

Hierarchical Error - Reducing log messages and maintaining context.

Downloads

86

Readme

hierarchical-error

NPM version NPM downloads Code Style

Hierarchical errors provide a structured and more efficient way of managing error logging in complex applications, particularly those with multiple layers of function calls, as is common in node applications.

By encapsulating the original error, its context, and any additional information at each layer of the call stack, hierarchical errors allow for a more comprehensive view of what went wrong without unnecessary duplication in the logs. This approach also ensures that relevant context is preserved, which can be crucial for debugging and understanding the flow of execution.

By using the HierarchicalError class, developers can maintain the integrity of the error message while preventing log clutter and improving the clarity of error reports, ultimately making troubleshooting much easier and more effective.

Another solution might be to use JavaScript’s Error class’s cause option as described here. Similar features are supported by other languages. However, this doesn’t support adding extra context to the error, without some type manipulation, and would require additional logic to extract the cause(s) for the logger when caught, which could be better encapsulated into a new type. This also wouldn’t allow for adding further explicit context, such as an indication of the logging level or HTTP response code for the error.

Read more

Install

npm i -save hierarchical-error

Example

const httpCall = () => {
    const someContext = 'the url we called';
    try {
        throw new Error('Something went wrong!');
    } catch (error: any) {
        throw new HierarchicalError(error.message, { someContext }, error);
    }
};

const callService = () => {
    const someContext = 'The service we called';
    try {
        httpCall();
    } catch (error: any) {
        throw new HierarchicalError(`Service failed.`, { someContext }, error);
    }
};

const getController = () => {
    try {
        callService();
    } catch (error: any) {
        if (isHierarchicalError(error)) {
            log(error.message, error.toJSON());
        } else {
            log(error);
        }
    }
};

Example Output

[ERROR] Service failed: Something went wrong! {
  message: 'Service failed.',
  context: { someContext: 'The service we called' },
  cause: {
    message: 'Something went wrong!',
    context: { url: 'http://example.com' },
    cause: Error: Something went wrong!
        at httpCall (/.../solution.js:19:15)
        at callService (/.../solution.js:28:9)
        at get (/.../solution.js:36:9)
        at main (/.../solution.js:48:5)
        ...
  }
}