0

I'm pretty new to express still and having some issues with rejected promises. To illustrate the issue I have a function using node pg-promise to query my database. Everything works fine if both entries exist, but if one does not it returns a rejected promise (as it is supposed to). My problem is what to do with the rejected promise. Right now my app crashes and logs the rejection.

I have error handling middleware that handles all errors by sending a response with the error message and status. This works fine for any other error I have thrown at it, but not rejected promises (which in fact never even reach it).

How can I handle rejected promises like these without my app crashing?

Here is my database function that returns the promise

 const transferColumnAmount = (fromTable, toTable, fromId, toId, column, amount) => {
    db.tx(t =>{
        return t.batch([
            t.one('UPDATE ${table:name} SET ${column:name} = ${column:name} + ${amount:csv} WHERE id = ${id:csv} RETURNING *', {
                table: fromTable,
                column: column,
                amount: -amount,
                id: fromId
            }),
            t.one('UPDATE ${table:name} SET ${column:name} = ${column:name} + ${amount:csv} WHERE id = ${id:csv} RETURNING *', {
                table: toTable,
                column: column,
                amount: amount,
                id: toId
            })
        ]);
    }).then(result => {
         return result;
    }).catch(err => {
        throw err;
    });
 };

Here is the middleware that uses it

const transferEnvelopeBudgetByIds = async (req, res, next) => {
    try{
        req.updatedEnvelopes = await transferColumnAmount("envelopes", "envelopes", req.envelopeFromId, req.envelopeToId, "budget", req.transferBudget);
        next();
    }catch(err){
        next(err);
    }
};

Here is the error handling middleware

apiRouter.use((err, req, res, next) => {
    if(!err.status){
      err.status = 500;
    }
    res.status(err.status).send(err.message);
  });
Brenden
  • 60
  • 7
  • 1
    In your `transferColumnAmount` function you forgot to `return` the promise – Bergi Jul 13 '23 at 20:07
  • 1
    Also [drop the pointless `.then(result => { return result; }).catch(err => { throw err; })`](https://stackoverflow.com/q/41089122/1048572) – Bergi Jul 13 '23 at 20:11
  • @Bergi Thanks! As in add a return before db.tx()? That seems to get me closer. I also removed the .then and .catch. Now things move along better, but no error is passed to the middleware and the sent response is {"status": 400} and it seems to still be missing my error handling middleware. Is there a way to pass the info from rejected promise to the error handling middleware? – Brenden Jul 13 '23 at 20:25
  • Please [edit] your question to include the code of the error handling middleware as well as the code that installs the two middlewares – Bergi Jul 13 '23 at 20:33
  • 1
    @Bergi I have no idea, but it's magically working now, for the life of my I don't think I changed anything besides what you suggested, so Im quite confounded. Anyway, it is all working now as it should, all because of the forgotten return. Thanks so much for finding that! I added the error handling middleware as well just in case it will help anyone else that comes across the post. Thanks again – Brenden Jul 13 '23 at 20:37

0 Answers0