Please help - Uncaught exception on an async function

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:

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.

Sample Function Node:

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

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.

This is the async function thats getting called.

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

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?

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 ?

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.

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.

1 Like

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