无法与React/Redux捕获错误
我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,并且还遇到了我的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
没有必要在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.