0

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.

Sam Sverko
  • 1,480
  • 4
  • 15
  • 32
  • What is the value of `promiseRes` that you were not expecting? – Bergi Sep 09 '19 at 20:12
  • promiseRes always returns something, making the catch(err) never firing even if the promise is returned an error. Do I have to define what promiseRes should be looking for? – Sam Sverko Sep 09 '19 at 20:29
  • Inside the promiseRes, I basically have an if statement that filters `if promiseRes[0].name == 'error'` then throw an error. But shouldnt the catch handle that natively? – Sam Sverko Sep 09 '19 at 20:30
  • Is 400 status an "error"? Can you check the http status of your result? – Johan Kvint Sep 09 '19 at 20:30
  • @SamSverko Yes, `promiseRes` did have some value, that's why the `catch` wasn't firing. If you can show us the value that you did receive and log, we might be able to tell you *why* that value was taken as a result and not considered to be an error by `request`, despite you claiming that you sent a 400 error. – Bergi Sep 09 '19 at 20:32
  • The promise will always return something, it's a "promise" right? As far as I know you need to reject in order to catch errors. – EternalHour Sep 09 '19 at 20:32
  • The response is a super long object: https://gist.github.com/SamSverko/8c341e4e25a4b16000929ee86e860756 – Sam Sverko Sep 09 '19 at 20:36
  • @SamSverko Thanks. The response object you see has `statusCode: 400` and `body: '{"name":"error","psql_error_code":"42P01"}'` though, so it seems this is expected behaviour of `request`. – Bergi Sep 09 '19 at 22:28

1 Answers1

0

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.