# Please help - Uncaught exception on an async function

**URL:** <https://discourse.nodered.org/t/please-help-uncaught-exception-on-an-async-function/33046>\
**Category:** General\
**Created:** [18 September 2020 13:57 UTC](https://discourse.nodered.org/t/please-help-uncaught-exception-on-an-async-function/33046 "2020-09-18T13:57:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonytroy](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@tonytroy](https://discourse.nodered.org/u/tonytroy)\
**Post date:** [18 September 2020 13:57 UTC](https://discourse.nodered.org/t/please-help-uncaught-exception-on-an-async-function/33046/1 "2020-09-18T13:57:19Z")

</div>

I am embedding node-red in an express application. The entire application logic is split into small functions within Node.js and these functions are called from Node-Red. The idea is to have Node-Red as an orchestration layer. There are some async function calls from Node-Red function nodes. Now when an error(uncaught) is thrown from the node.js function, the node-red function node does not catch it and this completely stops. The node-red layer is supposed to function like a scheduler checking the database for every 20 seconds for records to process.

**Also read this article:**

> **[Handling errors : Node-RED](https://nodered.org/docs/user-guide/handling-errors#uncatchable-errors)**

**Problems**

1. Node-Red function node has a catch. But the error still goes uncaught and node-red stops. How can I prevent this.

2. Also, if the node-red stops, is there a way to start it programmatically. Right now, i have to restart the entire express application which might not be feasible in Production environments.

3. My design is to have node-red flow run continuously. Hopefully its not a bad design.

Sample Design Below to showcase the idea.

 ![Screen Shot 2020-09-18 at 9.54.30 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/58dce4e7c6e247f9c4a7f38580fe8f89046d84c3.png)

Sample Function Node:

 ![Screen Shot 2020-09-18 at 9.55.35 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d9d3402a636ad77d09b18c5d26fca0f0aa950961.png)

_Please help. I have encountered this issue at the very last moment. I am going to Prod deployment in 2 weeks time._

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [18 September 2020 14:09 UTC](https://discourse.nodered.org/t/please-help-uncaught-exception-on-an-async-function/33046/2 "2020-09-18T14:09:36Z")

</div>

What is the actual error you are getting?

It may be you need to add proper error handling in a lower level than what you've shown here.

An uncaught async error is usually a sign of a missing handler for the 'error' event on some object.

---

<div class="post-metadata">

**Author:** ![tonytroy](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@tonytroy](https://discourse.nodered.org/u/tonytroy)\
**Post date:** [18 September 2020 14:17 UTC](https://discourse.nodered.org/t/please-help-uncaught-exception-on-an-async-function/33046/3 "2020-09-18T14:17:58Z")

</div>

> [@knolleary](#):
>
> here

**This is the async function thats getting called.**

```auto
const pdfGenSvcCall = async appData => {
    logger.info(`pdfGenSvcCall - Start`);
    const htmlForPdf = htmlDocumentBuilder(appData);
    logger.info(' **HTML for PDF generated'** );

    return (
      axios
        .post(pdfGenSvcUrl, htmlForPdf, { responseType: 'arraybuffer' })
        .then(response => {
          return Buffer.from(response.data, 'binary').toString('base64');
        })
        .catch(error => {
          logger.info('pdfGenSvcCall - Catch All');
          const { message, name, stack } = error;
          logger.debug(`============= API Repo Error Name as follows ==== ${name}`);
          logger.debug(`============= API Repo Error Message as follows ====${message}`);
          logger.debug(`============= API Repo StackTrace as follows ==== ${stack}`);
          throw new Error(error.message);
        })
    );

```

Axios just hangs for some reason and it doesnt even go to the catch block. The logs prints the below.  
[SCHEDULER] {"@timestamp":"2020-09-18T14:06:53.352Z","severity":"INFO","pid":20384,"message":" **pdfGenSvcCall - Start**"}  
[SCHEDULER] {"@timestamp":"2020-09-18T14:06:53.471Z","severity":"INFO","pid":20384,"message":"HTML for PDF generated"}

**Nothing is printed after this !!**

What happens in AXIOS is another issue that i am pursuing. So to answer your question, i dont know what error is being thrown. But this stops the node-red flow.

Also when you mention that I need to add proper error handling at a lower level, can you please be more specific. It helps !!

Thank you

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [18 September 2020 14:31 UTC](https://discourse.nodered.org/t/please-help-uncaught-exception-on-an-async-function/33046/4 "2020-09-18T14:31:09Z")

</div>

You said there is an uncaught exception and it is causing Node-RED to stop - so I was expecting you to have a stack trace showing the uncaught exception which would give us the clues needed to help point in the right direction.

Do you mean it causes the flow to stop processing or does the node-red process actually exit?

---

<div class="post-metadata">

**Author:** ![tonytroy](https://avatars.discourse-cdn.com/v4/letter/t/bc8723/32.png) [@tonytroy](https://discourse.nodered.org/u/tonytroy)\
**Post date:** [18 September 2020 15:04 UTC](https://discourse.nodered.org/t/please-help-uncaught-exception-on-an-async-function/33046/5 "2020-09-18T15:04:38Z")

</div>

Unfortunately i am unable to replicate the issue at the moment. I will keep trying and will update this thread. I am not sure how to differentiate between node-red process stopping or the flow stopping. The flow doesnt start again either ways though.

However when i read the above mentioned article on Handling Errors, the below line says

_ **"The typical cause will be that a node has kicked off an asynchronous task and that task has hit an error. A well-written node will have registered an error handler for that task, but if there isn’t one, the error will go uncaught."** _

Since my function node has a catch block, i am hoping that catches all errors coming from the node.js async function. Will that suffice ?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [18 September 2020 15:33 UTC](https://discourse.nodered.org/t/please-help-uncaught-exception-on-an-async-function/33046/6 "2020-09-18T15:33:41Z")

</div>

> [@tonytroy](#):
>
> Since my function node has a catch block, i am hoping that catches all errors coming from the node.js async function. Will that suffice ?

It will catch exceptions being thrown by the code you are calling. If that code does its own async work, you are reliant on it handling any errors in its own async code.

For example, if the code created a TCP socket but didn't register a handler for the 'error' event on that socket object, then _any_ async errors inside the TCP code will be left uncaught and cause Node-RED to exit.

> [@tonytroy](#):
>
> I am not sure how to differentiate between node-red process stopping or the flow stopping.

If the node-red process has stopped due to an uncaught exception, then the log will tell you very clearly.

If the flow is no longer processing messages, but Node-RED is still running (ie you can still see a node process) then that's an issue with your flow and the logic therein.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 November 2020 15:33 UTC](https://discourse.nodered.org/t/please-help-uncaught-exception-on-an-async-function/33046/7 "2020-11-17T15:33:51Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
