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

114
Views
ES6 way to wrap every express controller in a try catch block

I have an express app that has controllers that look like this:

export const createObject = async (req, res) => {
  try {
    ...do stuff
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};
export const updateObject = async (req, res) => {
  try {
    ...do stuff
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};
... etc

is there a fancy ES6 way to wrap every one of those calls in the try-catch block? I want to do something like:

const tryCatchWrapper = (f, res, code) => {
  return () => {
    try {
      return f.apply(this, arguments);
    } catch (e) {
      res.status(code).json(e);
    }
  };
};
export const createObject = tryCatchWrapper(async (req, res) => {
    ...do stuff
  }
}, res, 500);

but I'm not quite sure how to construct/compose this.

about 4 years ago · Juan Pablo Isaza
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!