(Ugly) solution for complete stack traces in async/await. Production ready.
We needed complete stack traces that has a very low performance overhead. This mini-module is created as a response to the closed issue Missing stack traces from async functions after the first await. I hope that NodeJS team will create a native solution.
It may be ugly but you can refactor your codebase quickly to use this module AND also you can quickly refactor it back
to the original async/await
.
npm install await-trace --save
Replace your await promise
with await nab(() => E(), promise)
. For example, convert this:
async function sideEffect(dummy){
const data = await fetchData()
return await updateData(data)
}
to this:
const {nab, E} = require('await-trace')
async function sideEffect(dummy){
const data = await nab(() => E(), fetchData())
return await nab(() => E(), updateData(data))
}
If you want to quickly refactor your code, I recommend you to use a replace tool that accepts regex patterns. I have done my refactoring using Jetbrains IDEs:
Find: await\s(.*)(\)$)
Replace: await nab(() => E(), $1$2)
E()
is an alias to Error()
. Module exports Err()
too.() => E()
is necessary in order not to degrade performance. E()
in every single nab()
setTimeout()
. With promises you'll be fine.--stack-trace-limit
flag, e.g.:
node --stack-trace-limit=100 index.js
NodeJS is great for development and this is just a pain in the ass in otherwise wonderful ecosystem! We'll be glad if you find any improvements (make a PR. ;) or if you offer a better solution than this one.