Sam Sverko
Sam Sverko

Reputation: 1600

Promise resolves when it should be catching error

I have a simple node+express app that makes a few async http/get url requests if you visit a certain page. This is to simply get some data from the db, and send that to the view. The routes are handled using a standard separate routes.js file.

routes.js

...
const bluebird = require('bluebird');
const promiseRequest = bluebird.promisify(require('request'));

Promise.all([
  promiseRequest(`http://url1...`),
  promiseRequest(`http://url2...`)
])
.then((promiseRes) => {
  res.render(...); // loads the view for the client
})
.catch((err) => {
  errorHandler(err, req, res); // to be handled by express middleware
});

The http url requests are handled using a controller file, which makes a query to the db, and returns either the values or an error.

controller.js

try {
  const {rows, rowCount} = await db.query(findAllQuery);
  return res.status(200).send({rows, rowCount});
} catch (error) {
  return res.status(400).send({
    "name": error.name,
    "psql_error_code": error.code
  });
}

The controller is working fine, but I am purposely typing in a wrong get url so that the controller returns the res.status(400)... object back to the route. No matter what I return to the route, the promise resolves. I even tried returning a new Error('Error description'), but the promisify receives this as a resolution.

Any help would be deeply appreciated!

--

The response from the controller to the route is a lengthy object.

Upvotes: 0

Views: 59

Answers (1)

umang-malhotra
umang-malhotra

Reputation: 21

As I can not see how you have implemented errorHandler(). One thing I can think of that you should look for is that - if inside .catch(err){} of the called promise you are not returning any error, it will go to the then of the calling function.

Upvotes: 0

Related Questions