0

I'm having issues catching error when the functions are nested three levels deep. Here is a router with async functions:

router.post('/',
  validatevalues,
  async (req, res) => {
    // if values are invalid then
    // return res.status(422).send(errors.msg);

    const result = await userController.post(req.body);
    return res.status(201).send('Success');
  },
);

const userController = {
    async post(req) {
        try {
            await bcrypt.genSalt()
            await bcrypt.hash();
            await db.query(req.somevalues);
        }  catch (err) {
            console.error(err);
        };
    };
};
const query = {
    return new Promise(function(resolve, reject) {
      ...
      if (err) {
        reject(new Error(err));
      } else {
        resolve(res);
      };
    };
};

The console.error(err) is printing this stack trace

Error: error: duplicate key value violates unique constraint...

And then I get Uncaught AssertionError at the router level with Mocha testing:

Uncaught AssertionError: expected { Object (_events, _eventsCount, ...) } to have status code 422 but got 201

This seems expected since I am just console.error instead of throwing another newError at the controller level, but what do I need to do? If I throw another error, then wouldn't the stack trace be Error: error: error ...? This doesn't seem right to me.

1
  • 2
    You could just not catch the error in the controller at all. Catch it in your router, which can handle it by sending the right (422) response. Commented Sep 8, 2019 at 18:14

1 Answer 1

1

You should only catch at the highest level:

  router.post('/', validatevalues, async (req, res) => {
    try {
      const result = await userController.post(req.body);
      return res.status(201).send('Success');      
    } catch(error) {
      res.status(402).send(error.message);
    }
 });

If you still want to log at a lower level, you can rethrow the error:

    }  catch (err) {
        console.error(err);
        throw err;
    } // no semicolon here, its unneccessary
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.