Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在axios catch()块中重新抛出捕获的错误_Javascript_Reactjs_Axios_Redux Saga - Fatal编程技术网

Javascript 如何在axios catch()块中重新抛出捕获的错误

Javascript 如何在axios catch()块中重新抛出捕获的错误,javascript,reactjs,axios,redux-saga,Javascript,Reactjs,Axios,Redux Saga,在axios中,为什么在catch()中不允许抛出新错误()? 我正处于这样的需求中,如果服务器返回错误,catch块应该抛出一个错误,稍后将由reduxsaga处理,并将调度适当的操作 API调用: export const userSignupApi = userDetails => { const endpoint = `${URL_ROOT}${URN_SIGNUP}`; axios .post(endpoint, userDetails) .then

axios
中,为什么在
catch()中不允许
抛出新错误()?
我正处于这样的需求中,如果服务器返回错误,catch块应该抛出一个错误,稍后将由reduxsaga处理,并将调度适当的操作

API调用:

export const userSignupApi = userDetails => {
   const endpoint = `${URL_ROOT}${URN_SIGNUP}`;
    axios
    .post(endpoint, userDetails)
    .then(response => {
      console.log("Response: ", response);
      //do something with the response
    })
    .catch(error => {
      throw new Error(error.response.data.message);
    });
};
由于上面的catch块,我得到了
未处理的拒绝(错误)
。 以下是我处理这项业务的传奇故事:

import { call, takeLatest, put } from "redux-saga/effects";

function* handleUserSignup(action) {
  try {
    yield call(userSignupApi, action.userDetails);
    yield put(userSignupSuccess()); //dispatching success action
  } catch (error) {
    yield put(userSignupFail(error)); //dispatching error action
  }
}

function* watchUserSignup() {
  yield takeLatest(NEW_USER.SIGNUP, handleUserSignup);
}

编辑:为什么我想要上面的代码结构?因为这样可以很容易地对API代码和saga代码进行单元测试。

您使用的是
try catch
,因为您不想在浏览器控制台中抛出要在
catch
中处理的错误。在
catch
中抛出错误将删除其用途。如果要抛出error remove
请尝试catch
(这不是推荐的方法)

更新


For axios catch方法捕获api url引发的任何错误。如果您不想捕捉错误并在浏览器中显示它,可以删除捕捉块,也可以调用处理错误的操作。有关promise catch的更多信息,您可以参考在
userSignupAPI
中创建的promise没有在任何地方使用(甚至没有返回),因此当在
catch
中抛出错误时,promise链将解析为(未捕获的)拒绝的promise,从而导致错误

userSignupAPI
的调用者中,您应该
等待
调用,以便
中的
try
/
catch
将看到抛出的错误:

export const userSignupAPI = userDetails => {
   const endpoint = `${URL_ROOT}${URN_SIGNUP}`;
    return axios
    .post(endpoint, userDetails)
    .then(response => {
      console.log("Response: ", response);
    })
    .catch(error => {
      throw new Error(error.response.data.message);
    });
};

async function* handleUserSignup(action) {
  try {
    yield await call(userSignupApi, action.userDetails);
    yield put(userSignupSuccess()); //dispatching success action
  } catch (error) {
    yield put(userSignupFail(error)); //dispatching error action
  }
}

function* watchUserSignup() {
  yield takeLatest(NEW_USER.SIGNUP, handleUserSignup);
}

(确保
call
返回由
userSignupApi
返回的
Promise

我得到了这个工作。我完全错了。正如@certianPerformance所建议的,在阅读了一些问题和github问题之后,我找到了正确的方法来处理这个问题。我不应该返回api响应,而是应该返回
承诺

解决方案如下:

export const userSignupApi = userDetails => {
  const endpoint = `${URL_ROOT}${URN_SIGNUP}`;

  return axios.post(endpoint, userDetails);
};
传奇:


把问题再读一遍。它不是关于
try-catch
,而是关于
axios中的
promise
功能(userSignupApi,action.userDetails)
甚至没有向服务器发出请求(
userSignupApi
)。您的
调用功能是什么?问题代码中没有定义对不起,我没有提到。它由
redux saga/effects
import { call, takeLatest, put } from "redux-saga/effects";

function* handleUserSignup(action) {
  try {
    const response = yield call(userSignupApi, action.userDetails);
    response.then(response => {
      const location = response.headers.location;
      put(userSignupSuccess(location));
      put(newUserWelcomeNote("Welcome user"));
    });
  } catch (error) {
    const errorMessage = error.response.data.message;
    yield put(userSignupFail(errorMessage));
  }
}

function* watchUserSignup() {
  yield takeLatest(NEW_USER.SIGNUP, handleUserSignup);
}