无法与React/Redux捕获错误

发布于 2025-02-03 21:21:48 字数 823 浏览 4 评论 0原文

我使用Redux有一个登录错误处理,看起来很喜欢:

export const login = (params: any) => async (dispatch: Dispatch) => {
  try {
    const authData = await API.post("login", params);
    sessionStorage.setItem("access_token", authData?.data?.access_token);
    dispatch(ActionCreators.getTokens(authData?.data?.access_token));
  } catch (error) {
    throw error;
  }
};

然后,在我的组件中,我试图再次通过Try/Catch捕获错误,但是我无法访问响应。猜猜是因为尝试/捕获范围。

  const submitHandler = async (e: SyntheticEvent) => {
    e.preventDefault();
    try {
      var res: any = await dispatch(
        login({
          email,
          password,
          token,
        })
      );

      router.push("/dashboard");
    } catch (error) {
      setError(res?.response.data.message);
    }
  };

有什么想法如何解决?谢谢。

I have a login error handling with redux which looks likes this:

export const login = (params: any) => async (dispatch: Dispatch) => {
  try {
    const authData = await API.post("login", params);
    sessionStorage.setItem("access_token", authData?.data?.access_token);
    dispatch(ActionCreators.getTokens(authData?.data?.access_token));
  } catch (error) {
    throw error;
  }
};

Then in my component, I am trying to catch the error with try/catch again, but I do not have an access to response. Guess because of try/catch scope.

  const submitHandler = async (e: SyntheticEvent) => {
    e.preventDefault();
    try {
      var res: any = await dispatch(
        login({
          email,
          password,
          token,
        })
      );

      router.push("/dashboard");
    } catch (error) {
      setError(res?.response.data.message);
    }
  };

Any idea how to fix that? Thank you.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2025-02-10 21:21:48

我遇到了同样的问题,并且还遇到了我的React组件和Redux组件文件中的错误。编译器无法正常工作,我从组件文件中删除了捕获块,并可以使用。请尝试,谢谢

I had hit same problem and was also catching error in both of my react component and redux component file. The compiler does not work like this, i removed the catch block from my component file and it was worked. Please try it, thanks

我一向站在原地 2025-02-10 21:21:48

没有必要在Redux Action的捕获块中丢弃错误。删除它有效。组件文件中的捕获块足以处理错误。

It is unnecessary to throw the error in catch block of redux action. Removing it works. Catch block in your component file is enough to handle error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文