I am currently developing a RESTful API with the help of Node.js and a client with React.
I am at the very beginning, ie the creation of a user via a registration form, and I realized that I do not really know how to handle the errors that my API sends me with React.
For example if I want to register with a username that is already taken, the controller of my API will throw an error :
throw {status: 422, message: "Username already used"}
My React client will retrieve it and display it in the console at the moment, but it displays a very general error (Request failed with status code 422) and not the message "Username already used"
_addUser(username, email, password)
.then((res) => {
console.log(res);
})
.catch((err) => {console.log(err.message)})
Does anyone have an idea of how to solve my problem? Or another way to handle errors?
res.status.