iam在这里使用react-redux
,当我使用trycatch
块时,它在执行错误时不会触发错误。为什么??
import {
LOGIN_REQUEST,
LOGIN_SUCCESS,
LOGIN_FAIL,
CLEAR_ERRORS,
} from "../constants/userConstant";
impo
rt axios from "axios";
export const login = (email, password) => async (dispatch) => {
try {
dispatch({ type: LOGIN_REQUEST });
const config = { headers: { "Content-Type": "application/json" } };
const { data } = await axios.post(
`/api/v1/login`,
{ email, password },
config
);
dispatch({ type: LOGIN_SUCCESS, payload: data.user });
} catch (error) {
dispatch({ type: LOGIN_FAIL, payload: error.response.data.message });
}
};
//Clearing errors
export const clearErrors = () => async (dispatch) => {
dispatch({ type: CLEAR_ERRORS });
};
=>它在react-redux-devtools
中显示LOGIN_FAIL
,但没有抛出错误
catch (error) {
dispatch({ type: LOGIN_FAIL, payload: error.response.data.message });
// Log your error in the console
console.log(error);
// throw error
throw error
}
如果你想控制台错误或抛出错误,可以这样做,但实际上你只是暂时调用你的调度。
try-catch块用于处理函数遇到的每一种情况,当出现错误时,它已被捕获,但您必须决定如何处理此错误。