Home > database >  How do I display my catch block message when there is no ID match?
How do I display my catch block message when there is no ID match?

Time:10-12

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?

CodePudding user response:

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  })
}

CodePudding user response:

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; }

CodePudding user response:

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);
});
  • Related