I have a normal function which called inside an API function.In API function i will call next(err) since I have an common error handler but how do I throw error in normal function without passing next in params?
const xxx = async (req, res, next) => {
try {
normalFunction(data);
} catch (err) {
next(err);
}
};
function normalFunction(data) {
try {
} catch (e) {
// how to throw error here??
}
}
CodePudding user response:
You can use 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, ...
}
}