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:
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.
We are seeing errors in production where this code in
socket.ts
:Calls this:
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:This will output:
Caught an error: Error A
, whereasError 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.