# Nakama-js throws uncatcheable exception

**URL:** <https://forum.heroiclabs.com/t/nakama-js-throws-uncatcheable-exception/5397>\
**Category:** Client Libraries\
**Tags:** sdk-javascript, typescript\
**Created:** [June 26, 2024, 4:03pm UTC](https://forum.heroiclabs.com/t/nakama-js-throws-uncatcheable-exception/5397 "2024-06-26T16:03:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thegoldenmule](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/thegoldenmule/32/2118_2.png) [@thegoldenmule](https://forum.heroiclabs.com/u/thegoldenmule)\
**Post date:** [June 26, 2024, 4:03pm UTC](https://forum.heroiclabs.com/t/nakama-js-throws-uncatcheable-exception/5397/1 "2024-06-26T16:03:59Z")

</div>

I made a [PR](https://github.com/heroiclabs/nakama-js/pull/196) for this, but the README says I should also post here. Here’s the issue:

We are seeing errors in production where this code in `socket.ts`:

```auto
this.adapter.onError = (evt) => {
        reject(evt);
        this.adapter.close();
};

```

Calls this:

```auto
close() {
    this._socket.close();
    this._socket = void 0;
}

```

But `this._socket` is undefined and throws an error. This throws a `TypeError` inside of a Promise constructor, which is then unhandled because the promise is already rejected. This can be demonstrated very succinctly with this bit of code:

```auto
const foo = () => new Promise((res, rej) => {
  rej('Error A');

  throw new Error('Error B');
});

const run = async () => {
  try {
    await foo();
  } catch(error) {
    console.log('Caught an error: ' + error);
  }
};

run();

```

This will output: `Caught an error: Error A`, whereas `Error B` **cannot be handled**.

To fix this, mirroring all other calls to `this.adapter.close()`, we simply need to wrap with a check that the adapter is indeed open.

---

<div class="post-metadata">

**Author:** ![thegoldenmule](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/thegoldenmule/32/2118_2.png) [@thegoldenmule](https://forum.heroiclabs.com/u/thegoldenmule)\
**Post date:** [July 3, 2024, 1:27pm UTC](https://forum.heroiclabs.com/t/nakama-js-throws-uncatcheable-exception/5397/2 "2024-07-03T13:27:11Z")

</div>

Does anyone know how we are supposed to report breaking bugs if not here?

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [July 4, 2024, 12:57pm UTC](https://forum.heroiclabs.com/t/nakama-js-throws-uncatcheable-exception/5397/3 "2024-07-04T12:57:12Z")

</div>

Hello @thegoldenmule,

The PR was accepted, thank you for the contribution.
