【问题标题】:How can i catch error? when i use redux-saga?我怎样才能捕捉到错误?当我使用 redux-saga 时?
【发布时间】:2021-05-10 19:00:22
【问题描述】:

如何捕捉错误?当我使用 redux-saga 时? 如果在 const refreshresult = yield call(refresh) 中发生错误;

我想停止并抛出这个错误动作

我不知道我能做什么

         yield put({
          type: REFRESH_FAILURE,
          error: err.response.data,
        });

我该怎么做?我应该如何停止并发送错误操作?

这是我的代码

        function getPostAPI(data) {
          return axiosInstace.post('/kakao/getpost', data);
        }

        function* getPost(action) {
          try {
          
            const result = yield call(getPostAPI, action.data);
            yield put({
              type: GETPOST_SUCCESS,
              data: result.data,
            });
          } catch (err) {
            if (err.response.data === 'jwtEx') {

         const refreshresult  =   yield call(refresh);  // <<<< if error occure refresh() i want to stop 

          
              yield put(action);
            } else {
              yield put({
                type: GETPOST_FAILURE,
                error: err.response.data,
              });
            }
          }
        }



        function* refresh() {
          try {
            const result = yield call(refreshAPI);
            yield AsyncStorage.setItem(
              'accesstoken',
              `${result.data.accessToken}`,
              () => {
                // console.log('accesstoken 재발급 저장 완료');
            
              },
            );
            yield put({
              type: REFRESH_SUCCESS,
              data: result.data,
            });
          } catch (err) {
          
            yield put({
              type: REFRESH_FAILURE,
              error: err.response.data,
            });
          }
        }

【问题讨论】:

    标签: reactjs react-native redux react-redux redux-saga


    【解决方案1】:

    我猜你的做法是对的。 Try and catch 会奏效。

    查看此链接了解更多信息Redux-saga documentation

    【讨论】:

      猜你喜欢
      • 2010-12-29
      • 1970-01-01
      • 2019-09-18
      • 2011-12-04
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多