Skip to content

Two util methods extendStack and extendCb that make it easier to track async errors in callback systems.

License

Notifications You must be signed in to change notification settings

martinheidegger/extend-stack

Repository files navigation

extend-stack

extend-stack offers two util methods extendStack and extendCb that make it easier to track async errors in callback systems.

Build Status

Installation and Setup

$ 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 either when calling your code…

$ env DEBUG=extend-stack node myapp.js # mac variant!

…or programmatically − after adding $ npm install debug − like this…

const debug = require('debug')
debug.enable('extend-stack')

Usage

The two methods are exposed as object:

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.

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:

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.

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

About

Two util methods extendStack and extendCb that make it easier to track async errors in callback systems.

Topics

Resources

License

Stars

Watchers

Forks