async function errorMiddleware(err, req, res, next) {
  if (res.headersSent) {
    // If you call next() with an error after you have started writing the response
    // (for example, if you encounter an error while streaming the response
    // to the client), the Express default error handler closes
    // the connection and fails the request.

    // So when you add a custom error handler, you must delegate
    // to the default Express error handler, when the headers
    // have already been sent to the client
    // Source: https://expressjs.com/en/guide/error-handling.html
    return next(err);
  }
  console.log(`Error on endpoint ${req.url}`, err.message, err.stack);
  res.status(500).send({ status: 'error', reason: 'internal-error' });
}
@param {Error} err
@param {import('express').Request} req
@param {import('express').Response} res
@param {import('express').NextFunction} next