How do you deal with errors?
Now with fetch you have a specific way of dealing with errors.
Now when you send the fetch request, then in the then part of the message.
Now earlier we saw that in the then part of the message you got the response and
then you dealt with the response.
Now the response from the server side could be a true response
with a status code between 200 and
299 which means that everything went very well on the server side.
But if the status code is in 400 to 500 range or something outside the 200
to 300 range then your server will simply send back the response message as such.
But with fetch, you have to explicitly check the response message to see
whether the status code is acceptable status code or the status
code is indicating some error from the server side and handle it appropriately.
Now fetch provides you with a method of doing this.
What fetch does is if the status code in the response
message is within the normal limits 200 to 299
then it will set a property call as OK on the response.
And so you can check this response, start OK to see if it is true or false.
If it is true, then the response that you get is a valid response.
And so you can simply return that response over to the next then that you chain
into the sequence of then there.
Otherwise, so this is the otherwise part, if it is not then you recognize that your
status code is outside the normal range that you would consider a normal
response from the server side, in that case we will handle it with the else part.
Here you have to explicitly create an error like this here,
so you are extracting from the response.
You're saying response.status, response.status contains the status code.
So for example, if you receive 404, the else part should be executed
because the response.OK will be set to false, so the else part will be executed.
And there you will construct an error object as shown here.
So you can extract the status text from the response and the status code
from the response and construct an error message string as shown here.
And then you can put that into the error object.
You can even put the response into the error object if you want and
then throw the error there.
When you throw the error, the catch that you'll implement later on
will catch this error and then deal with it appropriately.
So that's how you would handle the response from the server side.
Now if the server explicitly or
if in communicating with the server you experience an error.
So which means that you never even managed to communicate with the server.
In that case, fetch will generate an error response and
that is handled by giving a second callback function to the then.
Promises allow you to give two callback functions as parameters to the then.
The first callback function will be used when the promise resolves correctly.
The second callback function will be used when the promise is rejected.
So the error part is where the rejection of the promise is handled.
So if it is rejected, then you will create an error message from
the error message and then throw an error at that point.
That's another way of handling errors that are caused.
So this is fully your responsibility,
this part of the code is something that you implement when you use fetch.
Now some people that use other libraries, I'll talk about them a little bit later.