简体   繁体   English

当我已经在 catch 块中抛出错误时,得到未处理的 Promise 拒绝

[英]Getting unhandled Promise rejection when I already throw err in the catch block

In my redux I got this axios get call As you can see I already throw the err here在我的 redux 中,我得到了这个 axios 接电话如您所见,我已经在这里抛出了错误

export const getShippingMethodById = (token, id) => {
    return (dispatch) => {
        const config = {
            headers: {
                Authorization: `bearer ${token}`,
                'Content-Type': 'application/json',
            },
        };
        return axios
            .get(`${baseUri}/api/checkout/GetShippingById?addressId=${id}`, config)
            .then((res) => {
                dispatch({
                    type: FETCH_SHIPPING_METHOD_BY_ID,
                    payload: res.data.shippingInfos,
                });
            })
            .catch((err) => {
                console.log(err);
                throw err;
                alert('Cannot connect to server');
            });
    };
};

In the functional component, inside the useEffect hooks I am calling this function在功能组件中,在 useEffect 挂钩中,我将其称为 function

useEffect(() => {
    let splitText = cartList?.OrderDTO?.DeliveryCountry;
    let deliveryAddressId = splitText?.split(',');
    if (
      cartList.OrderDTO?.DeliveryCountry !== '' &&
      deliveryAddressId !== undefined
    ) {
      dispatch(
        getShippingMethodById(token.access_token, Number(deliveryAddressId[1])),
      ).then((res) => {
        console.log(res);
      });
    } else {
      dispatch(
        getShippingMethodById(
          token.access_token,
          cartList.OrderDTO?.CustomerAddressId,
        ),
      ).then((res) => {
        console.log(res);
      });
    }
  }, [cartList]);

But when ever this component is loaded I got this error但是当这个组件被加载时,我得到了这个错误

Since I already handle the promise rejection Why am I getting this error?由于我已经处理了 promise 拒绝为什么我会收到此错误?

错误截图

Because you are using throw err;因为您使用的是throw err; in catch block.catch块中。 You need to remove it.您需要将其删除。 You can read more about throw in here: https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Statements/throw您可以在此处阅读有关throw in 的更多信息: https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Statements/throw

Throwing an error from a catch won't be caught by the same catch block being executed.从一个catch中抛出一个错误不会被正在执行的同一个 catch 块捕获。 This means you are returning a Promise rejection to your UI code and the error should then be caught there.这意味着您正在向您的 UI 代码返回 Promise 拒绝,然后应该在此处捕获错误。 Add a catch to the Promise chain.向 Promise 链添加一个catch

useEffect(() => {
  let splitText = cartList?.OrderDTO?.DeliveryCountry;
  let deliveryAddressId = splitText?.split(',');
  if (
    cartList.OrderDTO?.DeliveryCountry !== '' &&
    deliveryAddressId !== undefined
  ) {
    dispatch(
      getShippingMethodById(token.access_token, Number(deliveryAddressId[1])),
    ).then((res) => {
      console.log(res);
    }).catch(error => {
      // handle rejected Promise or any other error from this chain
    });
  } else {
    dispatch(
      getShippingMethodById(
        token.access_token,
        cartList.OrderDTO?.CustomerAddressId,
      ),
    ).then((res) => {
      console.log(res);
    }).catch(error => {
      // handle rejected Promise or any other error from this chain
    });
  }
}, [cartList]);

Or simply remove rethrowing the error:或者干脆删除重新抛出错误:

return axios
  .get(`${baseUri}/api/checkout/GetShippingById?addressId=${id}`, config)
  .then((res) => {
    dispatch({
      type: FETCH_SHIPPING_METHOD_BY_ID,
      payload: res.data.shippingInfos,
    });
  })
  .catch((err) => {
    console.log(err);
    // no error rethrow
    alert('Cannot connect to server');
  });

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

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