How to handle error the request from the server?

There is a function which dispatches event and makes a request. Errors are treated inside using try/catch...
As I look to it to catch the error ?
Now doing so, but it flies in then (need to catch):
checkEmail({ email }).then((resolve) => {
 console.log("resolve:" + resolve);
})
 .catch((reject) => {
 console.log("reject:" + reject);
 });


The function itself:
export const checkEmail = ({ email }) => async (dispatch) => {
dispatch({
 type: FORM_LOADING,
});

 try {
 const config = {
 headers: {
 "Content-Type": "application/json",
},
};

 const res = await axios.get(`/api/users?email=${email}`, email, config);
dispatch({
 type: CHECK_EMAIL,
 payload: res.data
});

 } catch (err) {
 const errors = err.response.data.errors;
 if (errors) {
dispatch({
 type: FORM_ERROR,
});
 return errors[0].msg;
}
}
};
April 19th 20 at 12:34
1 answer
April 19th 20 at 12:36
Solution
throw the error further up

catch (err) {
 const errors = err.response.data.errors;
 if (errors) {
dispatch({
 type: FORM_ERROR,
});
 // return errors[0].msg;
 throw new Error(errors[0].msg)
}
 }
Grateful! - Israel.Boyer commented on April 19th 20 at 12:39

Find more questions by tags JavaScript