Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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??
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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, ...
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!