How can I send a node JS Axios request error message? I don't want to print the error in the console, I want to send it to the application.
For instance, if I am testing on postman and i hit localhost:8080/list/2
and 2
is the wrong id, instead of endlessly showing sending request
, I want the specific JSON error message returned.
**server.js** proxy server running on localhost:8000 makes request to another endpoint
app.get("/list/:id", function (req, res) {
const { id } = req.params;
axios
.get(`${BASE_URL}/list/` + id)
.then((response) => {
res.send(response.data)
})
.catch((error) => {
if(error.response) {
res.send(error.response)
}
});
});
2. How can this error message be used in an Axios request on the client side?
const getList = () => {
axios
.get("http://localhost:8000/list", {
params: {
id: '4'
}
})
.then((resp) => {
const data = resp.data;
console.log(resp.data)
})
.catch((err) => {
console.error(err);
});
};