如何在函数中调用错误处理程序?不是 API

How to call error handler in a function? Not an API

我有一个在 API function.In API 函数中调用的普通函数 我将调用 next(err) 因为我有一个通用的错误处理程序但是我该如何抛出错误在正常功能中没有传递参数中的 next?

const xxx = async (req, res, next) => {
  try {
    normalFunction(data);
  } catch (err) {
    next(err);
  }
};

function normalFunction(data) {
  try {
  } catch (e) {
    // how to throw error here??
  }
}

您可以使用throw

The throw statement throws a user-defined exception.
Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack.
If no catch block exists among caller functions, the program will terminate

function normalFunction(data) {
  try {
  } catch (e) {
    throw new Error(e); // Could also be a specific string, number, ...
  }
}