【问题标题】:How can I RETURN Axios error to client? (not only logging)如何将 Axios 错误返回给客户端? (不仅记录)
【发布时间】:2021-04-27 16:43:14
【问题描述】:

我知道这个问题已经被问过很多次了,但我面临的是一个非常烦人的问题。

我的服务器返回状态码为 500 的错误字符串。当我使用 axios 并捕获错误时,我可以轻松记录它,但是当我返回它时,我可以尝试一切,但它给了我未定义,或者它没有'不附加任何东西。

export const submitCheckout = async (betImport, ticket_id, token) => {
  const res = await axios({
    method: "post",
    url: rootUrl + "bets/checkout/" + ticket_id,
    headers: {
      "x-auth-token": token,
    },
    data: {
      betImport,
    },
  }).catch(({ response }) => {
    console.log(response.status) //this works
    return response;
  });
  return res.data;
};

//HERE I CALL THE FUNCTION

const res = await submitCheckout(sum, ticket_id, token);
  //here i can access only the body of the error, even if i try to append something to it. 
    if (res.ticket_id) {
      emptyTicket();
      setmodal({
        show: true,
        title: "SUCCESS",
        subtitle: "BET PLACED",
        maxwin: `${res.maxWin}`,
        ticketId: `${res.ticket_id}`,
        account_sum: `${res.account_sum}`,
      });
      ModifyAccountUser(user, res.account_sum);
    } else {
      setmodal({
        title: "ERROR",
        show: true,
        status: `${res.status}`,
        error: `${res}`,
      });
      if (res.toString().includes("Token")) history.push("/login");
    }
    
    //WHAT I WANT TO DO
   export const submitCheckout = async (betImport, ticket_id, token) => {
  const res = await axios({
    method: "post",
    url: rootUrl + "bets/checkout/" + ticket_id,
    headers: {
      "x-auth-token": token,
    },
    data: {
      betImport,
    },
  }).catch(({ response }) => {
    return {...response, response.status}; //this returns the body only, 
    return {res: response, status: response.status}; //this crashes,
    return response + "-"+ response.status}; //this was my desperation attack and failed as well
  });
  return res.data;
};
 
    

【问题讨论】:

    标签: javascript reactjs error-handling axios http-status-code-404


    【解决方案1】:

    你可以试试这个。

    try {
        let res = await Axios({
          method: {method},
          URL: {url},
          data: {body}
        });
    
        let data = res.data;
        return data;
      } catch (error) {
        console.log(error.response); // this is the main part. Use the response property from the error object
    
        return error.response;
      }
    

    【讨论】:

      【解决方案2】:

      抛出一个错误并将您的响应作为字符串放入其中。然后在你的 promise 中使用 error.message 访问它:

      async function foo(){
      try{
      const res = await ax.post("url", {params})
      return res
      }
      catch(err){
      throw new Error("my error message")
      }
      }
      
      //then
      
      foo()
      .then(res=> do stuff)
      .catch(err=> err.message)

      【讨论】:

        猜你喜欢
        • 2019-06-29
        • 2020-11-22
        • 1970-01-01
        • 2012-06-30
        • 2011-05-05
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 2012-10-16
        相关资源
        最近更新 更多