在 React 17.0.2 的 redux saga 中使用错误处理程序来提升错误

发布于 2025-01-10 03:49:31 字数 2111 浏览 2 评论 0原文

我在 redux-saga 中使用错误处理程序包装器,它位于 helpers 文件夹

src/helpers/index.ts

import { call, put } from "redux-saga/effects";
import { authActions } from "src/store";
import { snackbarActions } from "src/store/snackbar/actions";
import { cookies } from ".";

/*  SAFE SAGA CALL FOR ERROR HANDLING */
export function sagaWrapper(sagaFn: any, errorAction?: any) {
   return function* (): any {
       try {

           return yield call(sagaFn, arguments[0]);

       } catch (error: any) {
           console.log("Error:", error)
       }
   }
}

然后我在 saga

src/store/persons/saga.ts

import { sagaWrapper } from "src/helpers";
import { call, put, takeLatest } from "redux-saga/effects";
import { Service } from "src/services";
import { ActionType, ResponseType } from "src/types";
import { personActions } from "./actions";
import { PERSON_TYPES } from "./types";

const personService = new Service('persons');

function* search({ payload }: ActionType) {
    const response: ResponseType = yield call(personService.patchPath, 'search', payload.person);
    yield put(personActions.setSearch(response.data));
}

export function* personSagas() {
    yield takeLatest(PERSON_TYPES.SEARCH, sagaWrapper(search, personActions.setSearch()))
}

中使用它之后,我使用 rootSaga 并将中间件设置为还原商店。 当我运行开发服务器时,出现此错误:

io-6de156f3.js:111 TypeError: Cannot read properties of undefined (reading 'sagaWrapper')
    at Module.sagaWrapper (useQueryParams.ts:12:1)
    at personSagas (sagas.ts:16:1)
    at personSagas.next (<anonymous>)
    at next (redux-saga-core.esm.js:1157:1)
    at proc (redux-saga-core.esm.js:1108:1)
    at runEffect (redux-saga-core.esm.js:1199:1)
    at digestEffect (redux-saga-core.esm.js:1271:1)
    at redux-saga-core.esm.js:673:1
    at Array.forEach (<anonymous>)
    at runAllEffect (redux-saga-core.esm.js:672:1)

io-6de156f3.js:112 The above error occurred in task rootSaga
    created by rootSaga

我使用 create-react-app 和 React 17.0.2 版本,过去我创建了一个版本 17.0.0 的 React 应用程序,一切都运行良好。有什么解决办法吗?

谢谢。

Im using an error handler wrapper in redux-saga, this is in helpers folder

src/helpers/index.ts

import { call, put } from "redux-saga/effects";
import { authActions } from "src/store";
import { snackbarActions } from "src/store/snackbar/actions";
import { cookies } from ".";

/*  SAFE SAGA CALL FOR ERROR HANDLING */
export function sagaWrapper(sagaFn: any, errorAction?: any) {
   return function* (): any {
       try {

           return yield call(sagaFn, arguments[0]);

       } catch (error: any) {
           console.log("Error:", error)
       }
   }
}

Then I use it in a saga

src/store/persons/saga.ts

import { sagaWrapper } from "src/helpers";
import { call, put, takeLatest } from "redux-saga/effects";
import { Service } from "src/services";
import { ActionType, ResponseType } from "src/types";
import { personActions } from "./actions";
import { PERSON_TYPES } from "./types";

const personService = new Service('persons');

function* search({ payload }: ActionType) {
    const response: ResponseType = yield call(personService.patchPath, 'search', payload.person);
    yield put(personActions.setSearch(response.data));
}

export function* personSagas() {
    yield takeLatest(PERSON_TYPES.SEARCH, sagaWrapper(search, personActions.setSearch()))
}

After that, I use rootSaga and set the middleware in redux store.
When I run the development server I get this error:

io-6de156f3.js:111 TypeError: Cannot read properties of undefined (reading 'sagaWrapper')
    at Module.sagaWrapper (useQueryParams.ts:12:1)
    at personSagas (sagas.ts:16:1)
    at personSagas.next (<anonymous>)
    at next (redux-saga-core.esm.js:1157:1)
    at proc (redux-saga-core.esm.js:1108:1)
    at runEffect (redux-saga-core.esm.js:1199:1)
    at digestEffect (redux-saga-core.esm.js:1271:1)
    at redux-saga-core.esm.js:673:1
    at Array.forEach (<anonymous>)
    at runAllEffect (redux-saga-core.esm.js:672:1)

io-6de156f3.js:112 The above error occurred in task rootSaga
    created by rootSaga

Im using create-react-app with react 17.0.2 version, in the past I created a react app with version 17.0.0 and everything was working perfectly. Any solution?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文