Git Product home page Git Product logo

async-stacktrace's Introduction

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:

var ERR = require("async-stacktrace");

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

This is how you require the ERR function

var ERR = require("async-stacktrace");

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

async-stacktrace's People

Contributors

johnmclear avatar pita avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.