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

191
Views
How do I display my catch block message when there is no ID match?

I am getting data from an API and am displaying it on my local server.

Below is my code to get data which matches the ID from the API data:

router.get('/:id', async (req, res) => {
  checkString(req.params.id)
  try {
    const person = await peopleData.getPersonById(req.params.id);
    res.json(person);
  } catch (e) {
    res.status(404).json({ message: 'There is no person with that ID' });
  }

If there is no match I want to display the message like in the catch block, but the code does not go there as not getting a match is not an error technically.

So I tried the below code to get this message:

router.get('/:id', async (req, res) => {
  checkString(req.params.id)
  try {
    const person = await peopleData.getPersonById(req.params.id);
    if(!person) res.json('There is no person with that ID'); // Added new line here
    res.json(person);
  } catch (e) {
    res.status(404).json({ message: 'There is no person with that ID' });
  }

This does the work but it prints the message with quotes around as a string, is there a way I can display the message in the catch block if no match is found?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can throw an error and the catch will display it.

if(!person) throw new Error("There is no person with that ID");

....

then in the catch...

catch(e){
   res.status(404).json({ message: e.message  })
}
about 4 years ago · Juan Pablo Isaza Report

0

If you're sending people to a fullscreen "error stack" page, then you may not need to use res.json()! You can also use res.send()

if(!person){ res.send('<p>There is no person with that ID</p>'; return; }
// Or
if(!person){ res.send('There is no person with that ID'; return; }
about 4 years ago · Juan Pablo Isaza Report

0

You are returning Json responses, so it looks like your consumer is not a web page but another app. If so, you should return undefined or null if there is no person found, and let the web page or consumer decide what message to show. Reasons are:

  1. It should be easier to modify web pages than code, and typically the UI or marketing people will always want to fine tune (usually many times) every message on a web page.
  2. Your app is an API app. The place where the user not found message is to be shown can be many steps away. Or it may be inappropriate to show the message at all, for example the consuming app might want to redirect to/show a registration page instead if user is not found.
  3. Your web site may be multi-lingual, and you don't want the back-end to be involved in this.

"User not found" in many situations is not really an error, but it all depends on your application.

The catch block in your case should be used to handle other errors, for example, your database server might be down, or the database request might have timed out, etc etc. Your current code will misleadingly show "user not found" if there is a database error!

I would also let the Express error handler take care of such real errors, instead of coding error handling for every API function you have:

router.get('/:id', async (req, res, next) => {
  checkString(req.params.id);
  try {
    const person = await peopleData.getPersonById(req.params.id);
    res.json(person); // assuming getPersonById returns null if user not found
  } catch (e) {
    next(e);
});

Your Express error handler, where the invocation of the above next function lands, should be something like this (asssuming router is your Express app):

router.use((err, req, res, next) => {
  let statusCode = err.status || 500;
  // Assuming your app need to return only json responses
  res.json(err);
});
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!