googleapis/nodejs-vertexai

Errors from `appendHistory` promise are unhandled

Closed this issue ยท 2 comments

๐Ÿ‘‹ Hello!

https://github.com/googleapis/nodejs-vertexai/blob/a9dbb1da79be62f6527099978ee3dee479bd7237/src/models/chat_session.ts#L237C35-L237C48

This fire-and-forget promise includes a catch statement from the calling function. This catch statement does NOT forward the throw included in the caller because it is not awaited or returned.

This means that whenever there is an error in the fire-and-forget promise, it is not handled by the caller's try / catch context. Instead, it makes the program exit with code 1. So if the caller is trying to handle errors gracefully, the program exists with code 1. This caused downtime on our instance today when our auth wasn't configured correctly, the catch didn't work.

Here is a simplified reproduction in plain javascript:

const appendHistory = async () => {
  throw new Error("appendHistory exception");
};
const sendMessageStream = async () => {
  console.log("sendMessageStream");
  appendHistory().catch((e) => {
    throw new Error("appendHistory failed", e);
  });

  // a different promise is returned here
};
(async () => {
  try {
    sendMessageStream();
  } catch (e) {
    console.log(e);
  }
})();
mitchellhynes@Mitchells-MBP vertex-ai-poc % node test.js
sendMessageStream
/Users/mitchellhynes/work/vertex-ai-poc/test.js:9
    throw new Error('appendHistory failed', e);
          ^
Error: appendHistory failed
    at /Users/mitchellhynes/work/vertex-ai-poc/test.js:9:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v18.19.0
mitchellhynes@Mitchells-MBP vertex-ai-poc % echo $?
1

Hi @ecumene
PR #328 fixed the issue that breaks graceful error handling from caller program.

Hi I have released the fix to version 1.2.0