Ale*_*hin 5 javascript asynchronous node.js
假设您有一些相当复杂的node.js服务,有一天您会在其日志中看到类似的内容:
Error: getaddrinfo ENOTFOUND not-existing-host.com not-existing-host.com:80
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
Run Code Online (Sandbox Code Playgroud)
注意 - 没有堆栈跟踪.如果您的服务中有大量的地方进行http调用,您会如何找到地点错误?
是否有任何解决方案可以保持堆栈跟踪(我不关心性能影响,干净的堆栈跟踪更重要)?
似乎require('longjohn')可以帮助,但它在某种程度上工作不是非常可靠,有时不打印堆栈跟踪(不能创建一个示例来证明)是否有任何其他解决方案?
将生成此类错误的代码示例.我使用http作为示例,但对于任何异步函数都是如此.
const request = require('request');
// Let's say we write some helper function that wraps "request.js"
async function get(url) {
return new Promise((resolve, reject) => {
request(url, function (error, response, body) {
error ? reject(error) : resolve(body)
})
})
}
// Running
async function run() {
console.log(await get('http://not-existing-host.com'))
}
run()
.catch((e) => {
console.log(e.stack)
process.exit()
})
Run Code Online (Sandbox Code Playgroud)
PS
如果它可以帮助 - 我使用TypeScript,它提供了一些方法来检测异步/等待实现.目前我启用了ES8输出,因此TypeScripts输出node.js native async/await.
| 归档时间: |
|
| 查看次数: |
561 次 |
| 最近记录: |