Okay so this is what I find quite difficult most of the time. That is, to write proper error messages for the client to see when there is an error.
An example of a situation would be where I am using Axios and a third party api to get data. I need to write proper error messasges for both Axios' errors as well as errors from the api.
According to Axios' error handling documentation:
try {
} catch ( err ) {
if ( err.response ) {
// client received an error response (5xx, 4xx)
} else if ( err.request ) {
// client never received a response, or request never left
} else {
// anything else
}
}
And the third-party api's error is an object inside err.response called data. So therefore, my catch block looks like this:
try {
} catch ( err ) {
if ( err.response ) {
if ( err.response.data ) {
// api error
return;
}
// client received an error response (5xx, 4xx)
} else if ( err.request ) {
// client never received a response, or request never left
} else {
// anything else
}
}
For the API errors, I could simply use err.response.data.message as the messages are straight-forward and simple to understand for the users. But the problem is for the other type of errors that could arise from Axios or from my server. And Axios doesn't provide simple messages.
So what could I respond with in these situations? I could just respond with Something went wrong, please try again but that would be too vague.
Someone please help.