skip to Main Content

I catch an error in two places, still, it seems to propagate further on to my socket server, killing it. Below is a incomplete summary of my code. Apologies for failing to providing a true minimal reproducable example I could not trigger it in simple terms.

Outer catch

import WebSocket from 'ws';

export const run = async (ws: WebSocket) => {
  const executor = new Executor()
  
  const execution = executor.execute()

  try {
    for await(const update of execution) {
      ws.send(update.stringify())
    }
  } catch(error) {
    console.log("Outer catch", error)
  }
}

Executor (Inner catch)

class Executor {
    async *execute() {
        const promise = generator.next()

        // Handle run error
        promise.catch((error: Error) => {
          console.log("Inner catch", error) 
        })
    }
}

Result (order of errors)

Log: "Inner catch", <THE_SAME_ERROR>
Log: "Outer catch", <THE_SAME_ERROR>!!
Error: <THE_SAME_ERROR>!!!

[nodemon] app crashed

What are some reason this could be happening?

Edit

Im trowing the error on purpose, its as simple as

throw Error('Some message')

This error will be thrown in generator.next() above, which returns a promise that I don’t await. Hence, Im registering the catch callback just after.

2

Answers


  1. Chosen as BEST ANSWER

    Not a complete answer to my problem and I don't understand why, but I could fix my issue by chaining then and catch. Compare these examples:

    Failing

    (async () => {
      // The async generator
      const computer = async function *run() {
        throw Error('Some error!')
      }
    
      // Initialize the generator
      const runner = computer()
    
      // Store reference to the promise
      const promise = runner.next()
    
      // Handle success
      promise.then(() => {
        console.log("Handle success")
      })
    
      // Handle fail    
      promise.catch((error: Error) => {
        console.log("Catch", error)
      });
    
      // Await Error/Success
      await promise;
    })();
    

    Succeeding

    (async () => {
      // The async generator
      const computer = async function *run() {
        throw Error('Some error!')
      }
    
      // Initialize the generator
      const runner = computer()
    
      // Use chaining!
      const promise = runner.next()
        .then(() => {
          console.log("Handle success")
        })
        .catch((error: Error) => {
          console.log("Catch", error)
        });
    
      // Await Error/Success
      await promise;
    })();
    

  2. Could you try to expand your try/catch scope to:

    export const run = async (ws: WebSocket) => {
      try {
        const executor = new Executor();
    
        const execution = executor.execute();
    
        for await (const update of execution) {
          ws.send(update.stringify());
        }
      } catch (error) {
        console.log("Outer catch", error);
      }
    };
    

    And probably share a bit of context of your error message

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search