# Error Handling - Issues with Unhandled promise rejection

**URL:** <https://discourse.nodered.org/t/error-handling-issues-with-unhandled-promise-rejection/58163>\
**Category:** Developing Nodes\
**Created:** [11 February 2022 16:16 UTC](https://discourse.nodered.org/t/error-handling-issues-with-unhandled-promise-rejection/58163 "2022-02-11T16:16:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chromag](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@Chromag](https://discourse.nodered.org/u/Chromag)\
**Post date:** [11 February 2022 16:16 UTC](https://discourse.nodered.org/t/error-handling-issues-with-unhandled-promise-rejection/58163/1 "2022-02-11T16:16:56Z")

</div>

So I'm creating a node that's using Axios to make a call to an API. I've created an async function to make that call. I'm catching any exceptions thrown by that function but it is **still** throwing unhandled promise rejection errors. What am I missing? Here is the basic idea:

```auto
const axios = require("axios").default;

module.exports = (RED) => {
  const sendMessage = async ({ url, message }) => {
    axios.post(url, {
      message,
    });
  };

  function MyNode(config) {
    RED.nodes.createNode(this, config);
    const node = this;
    const message = "test";
    node.on("input", async (msg) => {
      await sendMessage({
        url: node.url,
        message,
      }).catch((ex) => {
        node.error(`error while sending message: ${ex.message}`);
      });

      node.send(msg);
    });
  }

  RED.nodes.registerType("mynode", MyNode);
};

```

I'm catching the async call. I'm not sure why it's throwing the unhandled promise rejection when the URL is invalid. I'm sure I'm missing something stupid but I can't tell what the issue is. I've tried putting try/catch blocks around anything and everything and it still throws the exception.

Am I missing some other async call? The node.on() event handler isn't catchable so maybe it's something related to the async function I'm using there?

Any ideas?

**EDIT**  
Here is the exact error:

```auto
(node:17) UnhandledPromiseRejectionWarning: Error: getaddrinfo ENOTFOUND whatever
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:71:26)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:17) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().

```

I've tried putting a try/catch block around the _axios.post_ since the exception seems to be coming from that call when it tries to call the URL provided but the error still persists.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 February 2022 16:36 UTC](https://discourse.nodered.org/t/error-handling-issues-with-unhandled-promise-rejection/58163/2 "2022-02-11T16:36:02Z")

</div>

Catching async errors can be tricky. This post explains the issue reasonably well, I think. [Error handling with Async/Await in JS | by Ian Segers | ITNEXT](https://itnext.io/error-handling-with-async-await-in-js-26c3f20bc06a)

---

<div class="post-metadata">

**Author:** ![Chromag](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@Chromag](https://discourse.nodered.org/u/Chromag)\
**Post date:** [11 February 2022 16:45 UTC](https://discourse.nodered.org/t/error-handling-issues-with-unhandled-promise-rejection/58163/3 "2022-02-11T16:45:58Z")

</div>

Indeed. As I suspected I missed one:

```auto
  const sendMessage = async ({ url, message }) => {
    await axios.post(url, {
      message,
    });
  };

```

I forgot to await the axios call (or return it's promise) so it was unhandled. I knew I must have missed something small 🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 February 2022 18:00 UTC](https://discourse.nodered.org/t/error-handling-issues-with-unhandled-promise-rejection/58163/4 "2022-02-11T18:00:31Z")

</div>

Yes, the first few times I tried to use async/await, I got myself in some terrible messes. I think I'm finally getting there now. The new package management library in uibuilder vNext is a good example. My Drayton Wiser node is also a good example, especially compared to my original node.js library.

Next step on the hard-to-learn list is migrating my nodes over to ESM from CommonJS modules 😖

---

<div class="post-metadata">

**Author:** ![Chromag](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@Chromag](https://discourse.nodered.org/u/Chromag)\
**Post date:** [11 February 2022 19:15 UTC](https://discourse.nodered.org/t/error-handling-issues-with-unhandled-promise-rejection/58163/5 "2022-02-11T19:15:10Z")

</div>

I'm writing a custom node and I'm planning on using ESM (right now I'm just using the CommonJS standard) but wasn't sure how much Node RED would have a fit if I tried it. I know in other (non-Node RED) projects I've done it and it was hit-or-miss with node (like having to enable experimental features in package.json).

Async await is great - when you don't do like I did and miss something 🙂

**EDIT** - Yeah I just noticed Node RED supports Node v12 and up? Can you even get ESM to work with 12? I know you can with 14 but you have to enable experimental support.

It appears as though the Node RED loader.js uses require and it does not like it if I convert my package to a module type!

**EDIT 2** - I just saw your post from a year ago about ESM modules being a mess so maybe I'll stick with ComonJS for now.

Due to how the loader uses require to load other modules how are you planning on converting your modules over to ESM? I'm poking around with it now but I'm not having any luck getting it to work (like renaming to .mjs or trying some other options like maybe using babel).

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 February 2022 21:36 UTC](https://discourse.nodered.org/t/error-handling-issues-with-unhandled-promise-rejection/58163/6 "2022-02-11T21:36:07Z")

</div>

> [@Chromag](#):
>
> I'm writing a custom node and I'm planning on using ESM (right now I'm just using the CommonJS standard) but wasn't sure how much Node RED would have a fit if I tried it. I know in other (non-Node RED) projects I've done it and it was hit-or-miss with node (like having to enable experimental features in package.json).

I've not tried it yet but now that node.js v12 is the minimum, I think it should be possible. It is now possible to require CommonJS modules from an ESM which I _think_ was the missing part. There are a few gottya's though - for example, you can't simply require a JSON file in an ESM like you can with CommonJS. However, the ESM library in node.js now lets you create a reference to the `require` function which helps.

> [@Chromag](#):
>
> Can you even get ESM to work with 12? I know you can with 14 but you have to enable experimental support.

No, I think they are native in both versions. You can also now specify the module type using the "type" property in package.json which saves messing with extension names.

[Modules: ECMAScript modules | Node.js v12.22.10 Documentation (nodejs.org)](https://nodejs.org/docs/latest-v12.x/api/esm.html)

[Modules: Packages | Node.js v12.22.10 Documentation (nodejs.org)](https://nodejs.org/docs/latest-v12.x/api/packages.html)

> [@Chromag](#):
>
> **EDIT 2** - I just saw your post from a year ago about ESM modules being a mess so maybe I'll stick with ComonJS for now.

Either things have changed or I'm getting better at node.js (or both!) because it looks like this is now feasible.

> [@Chromag](#):
>
> Due to how the loader uses require to load other modules how are you planning on converting your modules over to ESM? I'm poking around with it now but I'm not having any luck getting it to work (like renaming to .mjs or trying some other options like maybe using babel).

I don't see any point in using Babel, might as well stick with CommonJS, it isn't going away any time soon. I plan to use the type property in package.json. And then use [`module.createRequire`](https://nodejs.org/docs/latest-v12.x/api/module.html#module_module_createrequire_filename) where needed for CommonJS.

It seems doable now but still quite a bit of work. So it isn't high up on my todo list.

Some other things such as dynamic imports are also on the list to look at - that lazy loads modules that might only occasionally be needed. Potentially useful when working with something as complex as uibuilder.

> [@Chromag](#):
>
> It appears as though the Node RED loader.js uses require and it does not like it if I convert my package to a module type!

Right, that's useful to know. I'll probably start by converting some of my library modules, the nodes themselves will certainly be the last to go. It would help anyway since some of my dependencies are converting to ESM and it would be useful to be able to use the latest versions.

---

<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:** [12 April 2022 21:36 UTC](https://discourse.nodered.org/t/error-handling-issues-with-unhandled-promise-rejection/58163/7 "2022-04-12T21:36:20Z")

</div>

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