export function sendResponse()

in src/invoker.ts [43:75]


export function sendResponse(
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  result: any,
  err: Error | null,
  res: express.Response,
) {
  if (err) {
    sendCrashResponse({err, res, statusHeader: 'error'});
    return;
  }
  if (typeof result === 'undefined' || result === null) {
    res.sendStatus(204); // No Content
  } else if (typeof result === 'number') {
    // This isn't technically compliant but numbers otherwise cause us to set
    // the status code to that number instead of sending the number as a body.
    res.json(result);
  } else {
    try {
      res.send(result);
    } catch (sendErr) {
      // If a customer passes a non-serializeable object (e.g. one with a cycle)
      // then res.send will throw. Customers don't always put a lot of thought
      // into the return value because it's currently only used for
      // CallFunction. The most sane resolution here is to succeed the function
      // (this was the customer's clear intent) but send a 204 (NO CONTENT) and
      // log an error message explaining why their content wasn't sent.
      console.error(
        'Error serializing return value: ' + (sendErr as Error).toString(),
      );
      res.sendStatus(204); // No Content
    }
  }
}