{"_id":"async-stacktrace","_rev":"17-079f49509a2bb96e1cfd0b9e036641f0","name":"async-stacktrace","description":"Improves node.js stacktraces and makes it easier to handle errors","dist-tags":{"latest":"0.0.2"},"versions":{"0.0.1":{"name":"async-stacktrace","description":"Improves node.js stacktraces and makes it easier to handle errors","homepage":"https://github.com/Pita/node-async-stacktrace","author":{"name":"Peter 'Pita' Martischka","email":"petermartischka@googlemail.com"},"main":"./ERR","version":"0.0.1","_npmUser":{"name":"pita","email":"petermartischka@googlemail.com"},"_id":"async-stacktrace@0.0.1","devDependencies":{"vows":"0.6.0"},"dependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.0.106","_nodeVersion":"v0.4.12","_defaultsLoaded":true,"dist":{"shasum":"dd0a05b137bb6f293774b3fa8ce1676410f1c00d","tarball":"https://registry.npmjs.org/async-stacktrace/-/async-stacktrace-0.0.1.tgz","integrity":"sha512-sSQI3FT5Y7APxQT6EoPcrlJJfWcAYB+RyOb8hKxthx/nCDhRxjFHzfQRaXsnF6FDhev0sebOlvYy0mXAqoO4mg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCxXVGmkngVChFtwGeqXGDkoRpQob7r2SwrAQc8eDS2bwIhAMmUii92vL+rpd68ryMVKR7naR26K1O7f0KYbGMZH2bT"}]},"maintainers":[{"name":"pita","email":"petermartischka@googlemail.com"}],"directories":{}},"0.0.2":{"name":"async-stacktrace","description":"Improves node.js stacktraces and makes it easier to handle errors","homepage":"https://github.com/Pita/async-stacktrace","author":{"name":"Peter 'Pita' Martischka","email":"petermartischka@googlemail.com"},"devDependencies":{"vows":"0.6.0"},"main":"./ERR","version":"0.0.2","readme":"## Why classic stacktraces are not very helpful when dealing with async functions\n\nLook 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 \n\n```js\nfunction one()\n{\n   two(function(err){\n     if(err){\n       throw err;\n     }\n   \n     console.log(\"two finished\");\n   });\n}\n\nfunction two(callback)\n{\n  setTimeout(function () { \n    three(function(err)\n    {\n      if(err) {\n        callback(err);\n        return;\n      }\n      \n      console.log(\"three finished\");\n      callback();\n    });\n  }, 0);\n}\n\nfunction three(callback)\n{\n  setTimeout(function () { \n    four(function(err)\n    {\n      if(err) {\n        callback(err);\n        return;\n      } \n      \n      console.log(\"four finished\");\n      callback();\n    });\n  }, 0);\n}\n\nfunction four(callback)\n{\n  setTimeout(function(){\n    callback(new Error());\n  }, 0);\n}\n\none();\n```\n\n### When you execute it, you will get this:\n\n```\n$ node example_without.js \n\n/home/pita/Code/async-stacktrace/example_without.js:5\n       throw err;\n       ^\nError\n    at Timer.callback (/home/pita/Code/async-stacktrace/example_without.js:47:14)\n```\n\n### The problems here are:\n\n* You can see that the error happend in `four`, but you can't see from where `four` was called. The context gets lost\n* You write the same 4 lines over and over again, just to handle errors\n\n## The solution\n\n### Lets replace this code in `two` and `three` \n\n```js\nif(err) {\n  callback(err);\n  return;\n}\n```\n\n### with\n\n```js\nif(ERR(err, callback)) return;\n```\n\n### and replace this code in `one`\n\n```js\nif(err){\n  throw err;\n}\n```\n\n### with\n\n```js\nERR(err);\n```\n\n### This is how it looks like now: \n\n```js\nvar ERR = require(\"async-stacktrace\");\n\nfunction one()\n{\n   two(function(err){\n     ERR(err);\n   \n     console.log(\"two finished\");\n   });\n}\n\nfunction two(callback)\n{\n  setTimeout(function () { \n    three(function(err)\n    {\n      if(ERR(err, callback)) return;\n      \n      console.log(\"three finished\");\n      callback();\n    });\n  }, 0);\n}\n\nfunction three(callback)\n{\n  setTimeout(function () { \n    four(function(err)\n    {\n      if(ERR(err, callback)) return;\n      \n      console.log(\"four finished\");\n      callback();\n    });\n  }, 0);\n}\n\nfunction four(callback)\n{\n  setTimeout(function(){\n    callback(new Error());\n  }, 0);\n}\n\none();\n```\n\n### When you execute it, you will get this:\n\n```\n$ node example.js \n\n/home/pita/Code/async-stacktrace/ERR.js:57\n      throw err;\n      ^\nAsync Stacktrace:\n    at /home/pita/Code/async-stacktrace/example.js:6:6\n    at /home/pita/Code/async-stacktrace/example.js:17:10\n    at /home/pita/Code/async-stacktrace/example.js:30:10\n\nError\n    at Timer.callback (/home/pita/Code/async-stacktrace/example.js:41:14)\n```\n\n### What is new?\n\nThe \"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\n\n## npm\n```\nnpm install async-stacktrace\n```\n\n## Usage\n\nThis is how you require the ERR function\n\n```js\nvar ERR = require(\"async-stacktrace\");\n```\n\nThe parameters of `ERR()` are: \n\n1. `err` The error object (can be a string that describes the error too)\n2. `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\n\nThe return value is true if there is an error. Else its false\n","_id":"async-stacktrace@0.0.2","dist":{"shasum":"8bbb9787e3b38c836c729a7e9d7c08630db5d1ef","tarball":"https://registry.npmjs.org/async-stacktrace/-/async-stacktrace-0.0.2.tgz","integrity":"sha512-7aRR7I8Vlo3qEqeLzkLzlZHdQFQvWbhDjxeWKKUDI1d3WA6DXqGOlYAHYe0E5dS/uc5Nldyul+Z3EhFcKkPauw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIH8rAMNMMAxoOaGy9NNGFFNdpWDT0Nwh4SDAM5u31moRAiEAiaSUpBd4sHZ3Afqm0npN/B11sHM0aRdMEGsY2Wh1AvA="}]},"_npmVersion":"1.1.63","_npmUser":{"name":"pita","email":"petermartischka@googlemail.com"},"maintainers":[{"name":"pita","email":"petermartischka@googlemail.com"}]}},"readme":"## Why classic stacktraces are not very helpful when dealing with async functions\n\nLook 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 \n\n```js\nfunction one()\n{\n   two(function(err){\n     if(err){\n       throw err;\n     }  \n   \n     console.log(\"two finished\");\n   });\n}\n\nfunction two(callback)\n{\n  setTimeout(function () { \n    three(function(err)\n    {\n      if(err) {\n        callback(err);\n        return;\n      }   \n      \n      console.log(\"three finished\");\n      callback();\n    });\n  }, 0);\n}\n\nfunction three(callback)\n{\n  setTimeout(function () { \n    four(function(err)\n    {\n      if(err) {\n        callback(err);\n        return;\n      } \n      \n      console.log(\"four finished\");\n      callback();\n    });\n  }, 0);\n}\n\nfunction four(callback)\n{\n  setTimeout(function(){\n    callback(new Error());\n  }, 0);\n}\n\none();\n```\n\n### When you execute it, you will get this:\n\n```\n$ node example_without.js \n\n/home/pita/Code/async-stacktrace/example_without.js:5\n       throw err;\n       ^\nError\n    at Timer.callback (/home/pita/Code/async-stacktrace/example_without.js:47:14)\n```\n\n### The problems here are:\n\n* You can see that the error happend in `four`, but you can't see from where `four` was called. The context gets lost\n* You write the same 4 lines over and over again, just to handle errors\n\n## The solution\n\n### Lets replace this code in `two` and `three` \n\n```js\nif(err) {\n  callback(err);\n  return;\n}\n```\n\n### with\n\n```js\nif(ERR(err, callback)) return;\n```\n\n### and replace this code in `one`\n\n```js\nif(err){\n  throw err;\n}  \n```\n\n### with\n\n```js\nERR(err);\n```\n\n### This is how it looks like now: \n\n```js\nrequire(\"./ERR\");\n\nfunction one()\n{\n   two(function(err){\n     ERR(err);\n   \n     console.log(\"two finished\");\n   });\n}\n\nfunction two(callback)\n{\n  setTimeout(function () { \n    three(function(err)\n    {\n      if(ERR(err, callback)) return;\n      \n      console.log(\"three finished\");\n      callback();\n    });\n  }, 0);\n}\n\nfunction three(callback)\n{\n  setTimeout(function () { \n    four(function(err)\n    {\n      if(ERR(err, callback)) return;\n      \n      console.log(\"four finished\");\n      callback();\n    });\n  }, 0);\n}\n\nfunction four(callback)\n{\n  setTimeout(function(){\n    callback(new Error());\n  }, 0);\n}\n\none();\n```\n\n### When you execute it, you will get this:\n\n```\n$ node example.js \n\n/home/pita/Code/async-stacktrace/ERR.js:57\n      throw err;\n      ^\nAsync Stacktrace:\n    at /home/pita/Code/async-stacktrace/example.js:6:6\n    at /home/pita/Code/async-stacktrace/example.js:17:10\n    at /home/pita/Code/async-stacktrace/example.js:30:10\n\nError\n    at Timer.callback (/home/pita/Code/async-stacktrace/example.js:41:14)\n```\n\n### What is new?\n\nThe \"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\n\n## npm\n```\nnpm install async-stacktrace\n```\n\n## Usage\n\nThe `ERR` function is set global. All you have to add to your code is\n\n```js\nrequire(\"./ERR\");\n```\n\nThe parameters of `ERR()` are: \n\n1. `err` The error object (can be a string that describes the error too)\n2. `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\n\nThe return value is true if there is an error. Else its false\n","maintainers":[{"name":"pita","email":"petermartischka@googlemail.com"}],"time":{"modified":"2022-06-13T03:37:49.507Z","created":"2011-12-04T12:54:44.887Z","0.0.1":"2011-12-04T12:54:47.014Z","0.0.2":"2012-11-17T19:48:50.564Z"},"author":{"name":"Peter 'Pita' Martischka","email":"petermartischka@googlemail.com"},"users":{}}