admin管理员组文章数量:1355522
Let's suppose I want to use an async function foo
from an external library, which calls another async function bar
that throws an error that foo
does not handle:
// External library code (non-modifiable)
async function bar(){
throw Error("Error example");
}
async function foo() {
bar(); // <-- Note this is not awaited!!
}
If in my code I want to use foo
, is there any way I can handle the error thrown by bar
? This does not work, as foo
does not await for bar
:
// My code
async function myMain(){
try {
await foo();
} catch (error) {
console.log("Error captured in myMain:", error.message);
}
}
My bet is that the foo
function not awaiting bar
it's a bug in the external library. But until it's fixed, do I have any way to handle the error that it throws?
The code is inside an AWS Lambda, which causes 500 errors when it has unhandled exceptions. I need a way to handle the errors "gracefully" to avoid that.
I do not know why the external library calls the function that throws the error without properly handling it. But as I am forced to use it and I cannot modify its code, I think that using undhandledRejection
may be the only course of action. Although I do not like it. I want to handle that function's specific unhandled errors, but let the other ones pass, as they might be bugs in my code I would like to detect.
Let's suppose I want to use an async function foo
from an external library, which calls another async function bar
that throws an error that foo
does not handle:
// External library code (non-modifiable)
async function bar(){
throw Error("Error example");
}
async function foo() {
bar(); // <-- Note this is not awaited!!
}
If in my code I want to use foo
, is there any way I can handle the error thrown by bar
? This does not work, as foo
does not await for bar
:
// My code
async function myMain(){
try {
await foo();
} catch (error) {
console.log("Error captured in myMain:", error.message);
}
}
My bet is that the foo
function not awaiting bar
it's a bug in the external library. But until it's fixed, do I have any way to handle the error that it throws?
The code is inside an AWS Lambda, which causes 500 errors when it has unhandled exceptions. I need a way to handle the errors "gracefully" to avoid that.
I do not know why the external library calls the function that throws the error without properly handling it. But as I am forced to use it and I cannot modify its code, I think that using undhandledRejection
may be the only course of action. Although I do not like it. I want to handle that function's specific unhandled errors, but let the other ones pass, as they might be bugs in my code I would like to detect.
1 Answer
Reset to default -1There is no way to fix this inside myMain()
alone without modifying foo()
or bar()
directly, because the error is already unhandled at the foo()
level.
- The best fix is modifying
foo()
. (awaitingbar()
)
async function foo() {
await bar()
}
If modifying
foo()
is not allowed, you must handle it globallyHandling globally:
process.on('unhandledRejection', (error) => {
console.log("Caught an unhandled rejection:", error.message);
});
本文标签: javascriptCan I capture an external async function unhandled errorStack Overflow
版权声明:本文标题:javascript - Can I capture an external async function unhandled error? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743951617a2567394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
foo
async and why do you await it? The external library seems to contain multiple bugs. – jabaa Commented Mar 31 at 11:58foo
represents a function of an external library that does multiple things. But, because of a bug I guess, it does not always handle all the errors that can happen. – asmartin Commented Mar 31 at 12:03bar()
returns a promise that's discarded and rejected. I would call this a bug in the external library. – jabaa Commented Mar 31 at 12:13foo
function not awaitingbar
is a bug in the external library." - yes, absolutely. Make them fix it. There are some workarounds, but none of them good. – Bergi Commented Mar 31 at 12:56