3 回答

TA貢獻2036條經驗 獲得超8個贊
您正在將異步代碼與同步代碼相結合,請嘗試使用 asyncchron :
const postData = async (param) => {
try {
const result = await createProcessApiCall(param)
}
catch(err) {
setIsError(true);
}
};
或者同步:
const postData = (param) => {
createProcessApiCall(param)
.then((response) => {
setApiData(response.data.data);
setIsSuccess(response.data.isSuccess);
})
.catch((e) => {
setIsError(true);
});
};

TA貢獻1820條經驗 獲得超10個贊
axios.interceptors.response.use(res=>{return res}, (error) => {
if (error.response.status !== 401) {
throw error;
}
if (typeof error.response.data.error.name !== "undefined") {
//do something on the error
}
});
最好使用 axios 攔截器來捕獲錯誤

TA貢獻1806條經驗 獲得超5個贊
任何與 200-299 之間包含的序列不同的狀態代碼,您都需要捕獲:
const postData = async (param) => {
await createProcessApiCall(param)
.then((response) => {
setApiData(response.data.data);
setIsSuccess(response.data.isSuccess);
})
.catch((e) => {
// @TODO parse err
console.log(e.response);
setIsError(true);
});
};
添加回答
舉報