【问题标题】:Get axios Response object when error occurred with Redux SagaRedux Saga 发生错误时获取 axios 响应对象
【发布时间】:2018-12-30 00:55:13
【问题描述】:

这对我来说是新的,我试图弄清楚当 axios 使用 yield generator (redux-saga) 发生错误时如何检索请求正文

这是我正在使用的一段代码:

function requestLogin(user: ILoginUserPayload) {
  const loginUrl = `${config.apiUrl}/auth/logIn`;
  return axios.post(loginUrl, user);
}

export default function* watchAuthAction(): IterableIterator<any> {
  while (true) {
    const { payload }: { payload: ILoginUserPayload} = yield take(TYPES.LOGIN_REQUEST);

    console.log(`Payload`, payload);
    try {
      const response = yield requestLogin(payload);
      console.log(`Response`, response);
    } catch (error) {
      // According to my debug, error.request contains all the axios info
      console.log(error.request.response);
      yield put({ type: TYPES.LOGIN_FAILURE, error: error.request.response.message });
    }
  }
}

我用VSCode调试了错误内容,发现里面有axios的请求信息(我的意思是,我99%肯定axios会报错,就到这里了)

我的身体反应是这样的:

{"message":"User doesn't exist","stack":"Error: User doesn't exist"}

所以我试着用:

console.log(error.request.response.message);

但是不行,可能是我忘记了关于axios的一些事情……

有什么想法吗?

编辑:我实际上正在阅读这部分:https://github.com/axios/axios#handling-errors 但我仍然无法访问我的数据:/

【问题讨论】:

    标签: reactjs error-handling axios redux-saga


    【解决方案1】:

    您的代码似乎是正确的,对我来说唯一的区别是:

    try {
        const response = yield call(apiCall, action.payload)
          yield put({ type: successReducer, payload: response });
      } catch(error) {
        // Construct an error message
        let errorMessage = error.response.status + ":"+ error.response.statusText;
        yield put({ type: failReducer, payload: {  message : errorMessage }});
      }
    

    所以对我来说,区别似乎是我访问了 error.response.status error.request.response。

    【讨论】:

    • 是的,没错,我回复了关于它的信息(见其他答案;))
    【解决方案2】:

    所以正如我所料,这是一个 axios 错误。

    我只是按照文档获取数据:)

    这是部分:

    axios.get('/user/12345')
      .catch(function (error) {
        if (error.response) {
          // The request was made and the server responded with a status code
          // that falls out of the range of 2xx
          console.log(error.response.data);
          console.log(error.response.status);
          console.log(error.response.headers);
        } else if (error.request) {
          // The request was made but no response was received
          // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
          // http.ClientRequest in node.js
          console.log(error.request);
        } else {
          // Something happened in setting up the request that triggered an Error
          console.log('Error', error.message);
        }
        console.log(error.config);
      });
    

    在我的情况下,我只需要使用以下方式获取消息:

    error.response.data.message
    

    【讨论】:

    • 但是你如何在传奇中得到它?你需要从 axios 的 catch 方法中返回一些特定的东西吗? ...我试图返回任何可能的东西,但仍然在传奇中得到了一些奇怪的对象 ...
    • @peshohristov 您发现问题所在了吗?我也有同样的问题
    • @jbojcic - 是的,我得到了帮助我的答案,但这是我在redux-saga repo 中打开的问题。 github.com/redux-saga/redux-saga/issues/1730希望这对你有帮助:)
    • 谢谢。对我来说,问题出在调试器上。在调试 saga 时,它显示源映射代码,而堆栈跟踪和范围显示已编译代码,因此即使存在错误,我也无法在调试器中看到它。 github.com/redux-saga/redux-saga/issues/1972
    【解决方案3】:

    老兄,我花了好几个小时。

    这对我有用:

    console.log(error.response.data.message);
    

    所以,在您的error 对象中,有一个response 对象,然后您可以访问data 对象中的所有内容。

    try {
      const response = yield requestLogin(payload);
      console.log(`Response`, response);
    } catch (error) { 
      yield put({ type: TYPES.LOGIN_FAILURE, error: error.response.data.message });
    }
    

    你可以像这样在 catch 里面调试它:

    catch (error) {
        console.log('erro:', error.response.data.message) 
      }  
    

    【讨论】:

      猜你喜欢
      • 2019-06-08
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多