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

async-stacktrace

v0.0.2

Published

Improves node.js stacktraces and makes it easier to handle errors

Downloads

1,497

Readme

Why classic stacktraces are not very helpful when dealing with async functions

Look at this example. one calls two, two calls three, and three calls four. All functions call the given callback asynchronous. four calls the callback with an error. three and two passes the error to their callback function and stop executing with return. one finally throws it

function one()
{
   two(function(err){
     if(err){
       throw err;
     }  
   
     console.log("two finished");
   });
}

function two(callback)
{
  setTimeout(function () { 
    three(function(err)
    {
      if(err) {
        callback(err);
        return;
      }   
      
      console.log("three finished");
      callback();
    });
  }, 0);
}

function three(callback)
{
  setTimeout(function () { 
    four(function(err)
    {
      if(err) {
        callback(err);
        return;
      } 
      
      console.log("four finished");
      callback();
    });
  }, 0);
}

function four(callback)
{
  setTimeout(function(){
    callback(new Error());
  }, 0);
}

one();

When you execute it, you will get this:

$ node example_without.js 

/home/pita/Code/async-stacktrace/example_without.js:5
       throw err;
       ^
Error
    at Timer.callback (/home/pita/Code/async-stacktrace/example_without.js:47:14)

The problems here are:

  • You can see that the error happend in four, but you can't see from where four was called. The context gets lost
  • You write the same 4 lines over and over again, just to handle errors

The solution

Lets replace this code in two and three

if(err) {
  callback(err);
  return;
}

with

if(ERR(err, callback)) return;

and replace this code in one

if(err){
  throw err;
}  

with

ERR(err);

This is how it looks like now:

require("./ERR");

function one()
{
   two(function(err){
     ERR(err);
   
     console.log("two finished");
   });
}

function two(callback)
{
  setTimeout(function () { 
    three(function(err)
    {
      if(ERR(err, callback)) return;
      
      console.log("three finished");
      callback();
    });
  }, 0);
}

function three(callback)
{
  setTimeout(function () { 
    four(function(err)
    {
      if(ERR(err, callback)) return;
      
      console.log("four finished");
      callback();
    });
  }, 0);
}

function four(callback)
{
  setTimeout(function(){
    callback(new Error());
  }, 0);
}

one();

When you execute it, you will get this:

$ node example.js 

/home/pita/Code/async-stacktrace/ERR.js:57
      throw err;
      ^
Async Stacktrace:
    at /home/pita/Code/async-stacktrace/example.js:6:6
    at /home/pita/Code/async-stacktrace/example.js:17:10
    at /home/pita/Code/async-stacktrace/example.js:30:10

Error
    at Timer.callback (/home/pita/Code/async-stacktrace/example.js:41:14)

What is new?

The "Async Stacktrace" shows you where this error was caught and passed to the next callback. This allows you to see from where four was called. You also have less code to write

npm

npm install async-stacktrace

Usage

The ERR function is set global. All you have to add to your code is

require("./ERR");

The parameters of ERR() are:

  1. err The error object (can be a string that describes the error too)
  2. callback (optional) If the callback is set and an error is passed, it will call the callback with the modified stacktrace. Else it will throw the error

The return value is true if there is an error. Else its false