最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I capture an external async function unhandled error? - Stack Overflow

programmeradmin3浏览0评论

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.

Share Improve this question edited Mar 31 at 12:51 jabaa 6,9693 gold badges15 silver badges39 bronze badges asked Mar 31 at 11:17 asmartinasmartin 5281 gold badge3 silver badges18 bronze badges 11
  • Why is foo async and why do you await it? The external library seems to contain multiple bugs. – jabaa Commented Mar 31 at 11:58
  • @jabaa This is a simplified example. foo 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:03
  • 2 @Yogi In those questions, if I understand them correctly, the async functions that may throw errors are "awaited". So although they are not inside try-catch clauses, they are inside the promise chain. In my case, the function that throws the error is not awaited nor is part or a .then().catch() chain (I think because of a bug on their part). – asmartin Commented Mar 31 at 12:11
  • Either you've oversimplified the code or you can't handle the error. bar() returns a promise that's discarded and rejected. I would call this a bug in the external library. – jabaa Commented Mar 31 at 12:13
  • 1 "My bet is that the foo function not awaiting bar 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
 |  Show 6 more comments

1 Answer 1

Reset to default -1

There 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(). (awaiting bar())
async function foo() {
    await bar()
}
  • If modifying foo() is not allowed, you must handle it globally

    Handling globally:

process.on('unhandledRejection', (error) => {
    console.log("Caught an unhandled rejection:", error.message);
});
发布评论

评论列表(0)

  1. 暂无评论