The situation is such that I use the axios library and interceptors of successful and unsuccessful requests in order to automatically pick up and display an alert with the error text from the server in case of an error.
So my backend developer sends me error messages that are not suitable for output to an alert arguing that the message is I have to generate the error myself, but what comes from the server is only needed so that I know about the error. Is he right, or sending an error message from the backend should go strictly like response.message and match the error format displayed to the user on the UI?
The universal error interceptor that I spoke about looks like this, but the errors that it sends to me each time are on a different path, while their format is not suitable for displaying to the user, and often an array with errors comes to the request. Is he doing the right thing?
Because in that case, I cannot use the code below to handle errors
const API = axios.create({
baseURL: '/api/',
responseType: 'json',
headers: { 'cache': 'no-store' },
})
const onFulfilled = (response) => response.data
const onRejected = (error) => {
if (error.response?.status === 500) {
notify(error.response.statusText || ' Internal server error', 'error')
} else if (error.response?.data.message) {
notify(error.response.data.message, 'error')
} else {
notify('Oops something went wrong', 'error')
}
..... and so on
return Promise.reject(error)
}
API.interceptors.response.use(onFulfilled, onRejected)