https://github.com/martinheidegger/extend-stack
Two util methods extendStack and extendCb that make it easier to track async errors in callback systems.
https://github.com/martinheidegger/extend-stack
async callback error nodejs stack v8
Last synced: 7 months ago
JSON representation
Two util methods extendStack and extendCb that make it easier to track async errors in callback systems.
- Host: GitHub
- URL: https://github.com/martinheidegger/extend-stack
- Owner: martinheidegger
- License: mit
- Created: 2018-12-13T05:49:36.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-05-14T06:09:32.000Z (over 4 years ago)
- Last Synced: 2025-02-05T00:42:03.314Z (9 months ago)
- Topics: async, callback, error, nodejs, stack, v8
- Language: JavaScript
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# extend-stack
`extend-stack` offers two util methods `extendStack` and `extendCb` that make it easier
to track async errors in callback systems.
[](https://travis-ci.org/martinheidegger/extend-stack)
## Installation and Setup
```shell
$ npm install extend-stack
```
`extend-stack` is **disabled by default** because it consumes quite a bit of resources to work.
To enable it you need enable [debug][debug-wiki] either when calling your code…
```shell
$ env DEBUG=extend-stack node myapp.js # mac variant!
```
…or programmatically − after adding `$ npm install debug` − like this…
```javascript
const debug = require('debug')
debug.enable('extend-stack')
```
[debug-wiki]: https://github.com/visionmedia/debug#windows-command-prompt-notes
## Usage
The two methods are exposed as object:
```javascript
const { extendStack, extendCb } = require('extend-stack')
```
### `extendCb(function (err, data) {})` → `function (err, data) {}`
`extendCb` adds the error stack of the calling function in case an error
occurs.
```javascript
const fs = require('fs')
setImmediate(() => {
fs.readFile('non-existing', extendCb((err) => {
console.log(err.stack)
}))
})
```
(disabled)
```
Error: ENOENT: no such file or directory, open 'non-existing'
```
(enabled)
```
Error: ENOENT: no such file or directory, open 'non-existing'
at Immediate.setImmediate (/extend-stack/examples/extendCb.js:5:31)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
```
If the error contains a stack, the stack will be part of it:
```javascript
const { extendCb } = require('..')
function errorProne (cb) {
setImmediate(() => cb(new Error('sample-error')))
}
function userFunction () {
errorProne(
extendCb(err => console.log(err.stack))
)
}
setImmediate(userFunction)
```
(disabled)
```
Error: sample-error
at Immediate.setImmediate [as _onImmediate] (/extend-stack/examples/extendCb-withStack.js:4:25)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
```
(enabled)
```
Error: sample-error
at Immediate.userFunction (/extend-stack/examples/extendCb-withStack.js:9:5)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
caused by:
at Immediate.setImmediate (/extend-stack/examples/extendCb-withStack.js:4:25)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
```
### `extendStack(err, [offset])` → `Error`
`extendStack` extends the stack stored in `err` and adds the current line's stack as well.
```javascript
const { extendStack } = require('..')
function subsystem (err) {
return extendStack(err)
}
setImmediate(() => {
const err = subsystem(new Error('some-error'))
console.log(err)
})
```
(disabled)
```
Error: some-error
at Immediate.setImmediate (/extend-stack/examples/extendStack.js:8:25)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
```
(enabled)
```
Error: some-error
at subsystem (/extend-stack/examples/extendStack.js:4:10)
at Immediate.setImmediate (/extend-stack/examples/extendStack.js:8:15)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
caused by:
at Immediate.setImmediate (/extend-stack/examples/extendStack.js:8:25)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
```
## License
[MIT](./LICENSE)