I try to connect to api using axios, in catch state I try to re-throw the error I got from fetching and I want to let my catch from surrounding trycatch to return custom message from that re-thrown error
and here are my console logs
Password is not valid
C:\backend\aplication\Controller\userController.js:20
throw Error(error.response.data.error.toString());
^
Error: Password is not valid
at C:\backend\aplication\Controller\userController.js:20:19
at processTicksAndRejections (node:internal/process/task_queues:96:5)
there "Password is not valid" is the custom error message I was talking about, so I want it to be re-thrown and catched in my trycatch
and that's my code
const Login = async (req, res) => {
const {email, password} = req.body;
try {
axios.post("http://localhost:5000/api/name/user/login", {
email: email,
password: "passwordsarewqewqd"
}).then((response) => {
console.log(response.data);
}, (error) => {
//here I log my custom message and I want to throw it to trycatch to respond correctly
console.log(error.response.data.error.toString());
throw Error(error.response.data.error.toString());
});
} catch (error) {
console.log(error);
res.status(400).json({error: error.message});
}
}
btw, why the code snippet sometimes is in color but sometimes is not?