简体   繁体   English

在基本 catch 块中取消处理承诺拒绝

[英]Unhandle promise rejection in a basic catch block

I'm trying to throw an error from a function to another and to send a res.status(402).我试图将错误从一个函数抛出到另一个函数并发送 res.status(402)。 I put it in a try-catch block in the most basic way but still got the error.我以最基本的方式将它放在 try-catch 块中,但仍然出现错误。

: Unhandled promise rejection. :未处理的承诺拒绝。 This error originated either by throwing inside of an async function without a catch block此错误是由于在没有 catch 块的情况下抛出异步函数而引起的

this is the calling function:这是调用函数:

 const validateAction = async (req, res) => { const { action, url, mail, orderId } = req.body; try { requestAction(action, url, mail, orderId); } catch (err) { console.error(err); res.status(402).send({ status: 402, error: "BUG", }); }

this is the function I want from where I want to throw the error:这是我想要从哪里抛出错误的函数:

 const requestAction = async (action, url, mail, oderId) => { const result = actions.find((actionFind) => actionFind.action === action); await axios({ //change the action to "add" method: "post", url: "XXXXXXXXXXXXXXXXXXXX", data: { key: "XXXXXXXXXXXXXXXXXXXXXXXX", action: "XXXXX", service: XXXXXX, quantity: XXXXXXXXXX, }, }).then( (response) => { if (response.data.error) { sendLog( ` ${response.data.error} --- URL:${url} ACTION:${action} QUANTITY:${result.quantity} ID:${result.id}`, "error" ); // here is the throw that's not working--------------------------------------------- throw response.data.error; //---------------------------------------------------------------------------------- } else { sendMail(mail); sendLog( ` Succesfully sent it --- URL:${url} ACTION:${action} QUANTITY:${result.quantity} ID:${result.id} To ${mail}`, "info" ); } }, (error) => {} ); };

I believe the answer is not complicated but I don't find it我相信答案并不复杂,但我没有找到

You need to add an additional catch in the promise chain, for example like this:您需要在承诺链中添加一个额外的catch ,例如:

const validateAction = async (req, res) => {
  const { action, url, mail, orderId } = req.body;
  requestAction(action, url, mail, orderId);
}
   

const requestAction = async (action, url, mail, oderId) => {
  const result = actions.find((actionFind) => actionFind.action === action);
  await axios({
    //change the action to "add"
    method: "post",
    url: "XXXXXXXXXXXXXXXXXXXX",
    data: {
      key: "XXXXXXXXXXXXXXXXXXXXXXXX",
      action: "XXXXX",
      service: XXXXXX,
      quantity: XXXXXXXXXX,
    },
  }).then(
    (response) => {
      if (response.data.error) {
        sendLog(
          ` ${response.data.error} --- URL:${url}   ACTION:${action}   QUANTITY:${result.quantity}   ID:${result.id}`,
          "error"
        );

        throw response.data.error;

      } else {
        sendMail(mail);
        sendLog(
          ` Succesfully sent it --- URL:${url}   ACTION:${action}   QUANTITY:${result.quantity}   ID:${result.id} To ${mail}`,
          "info"
        );
      }
    }).catch(() => {
       console.error(err);
        res.status(402).send({
          status: 402,
          error: "BUG",
        });
   });
};

Basically need to add an基本上需要添加一个

await So await requestAction(action, url, mail, orderId); await所以 await requestAction(action, url, mail, orderId);

before the request action call.在请求操作调用之前。 Thanks to Felix Kling感谢 Felix Kling

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM