I’m working on my api calls within my client code and I’m having some trouble on figuring out how to handle errors on the server. Specifically 4xx and 5xx errors. One of the solutions I came across was checking the ok property on the response object immediately after the request completes.
If that is false, I manually throw an error with a string message describing the error. This works well. It rejects the Promise and throws the error. However, it immediately terminates the execution and the code I have on my react method is never reached. I’m sure the reason this is the case is that I’m rejecting the promise, thus completing the life cycle of it.
This is because you are throwing the error to another promise, which immediately rejects. But it doesn’t look like you are catching that one. And if a promise doesn’t have a catch block, it will terminate execution
from mdn
The Promise returned by catch() is rejected if… returns a Promise which is itself rejected; otherwise, it is resolved.
Still, you should handle errors in the client and UI gracefully. Meaning that your response should always return some kind of result, so that React has something to render.
const response = await fetch(`${base}/login`,options).then((res) => {
console.log(res.ok)
if(!res.ok) {
throw new Error('The credentials you provided are invalid')
} else {
return res.json();
}
}).catch((err) => {
// return error object to the client.
return err
})
then in the client
if (err.message === 'The credentials you provided are invalid') // render something
@JM-Mendez You are absolutely right. However, the issue I’m experiencing is that when the promise is rejected and passed into the catch, undefined is being returned when my expected should be the error object
I ended up creating my own error handler in my api.js file but for one of the requests I used the try/catch block. That worked a lot better. It would still ‘terminate’ but I was able to access the Error object.