fetch resolves even if 404?
A fetch()
call is only rejected if the network request itself fails for some reason (host not found, no connection, server not responding, etc...).
Any result back from the server (404, 500, etc...) is considered a successful request from the promise point of view. Conceptually, you made a request from the server and the server answered you so from the networking point of view, the request finished successfully.
You need to then test that successful response to see if has the type of answer you wanted. If you want a 404 to be a rejection, you could code that yourself:
fetch('notExists').then(function(response) {
if (!response.ok) {
// make the promise be rejected if we didn't get a 2xx response
throw new Error("Not 2xx response", {cause: response});
} else {
// got the desired response
}
}).catch(function(err) {
// some error here
});
You could even make your own myFetch()
that just does this automatically for you (converts any non-200 response status to a rejection).What is the reason behind a resolved promise for a a completely badFirst off, server down will not generate a successful response - that will reject.
request ( non existing resource / server down).
A successful response is generated if you successfully connect to the server, send it a request and it returns a response (any response). As for "why" the designers of the fetch()
interface decided to base the rejection on this, it's a bit hard to say without talking to someone who was actually involved in the design of that interface, but it seems logical to me. This way the rejection tells you whether the request got through and got a valid response. It's up to your code to decide what to do with the response. You can, of course, create your own wrapper function that modifies that default behavior.
Fetch returning 404 error: GET urlthatIwasfetchinghere 404 (Not Found)
From MDN's HTTP status code documentation:
404 Not FoundThe 404 error code means that the website or endpoint that you requested does not exist, which is definitely expected if you request a non-existent page.
The server can not find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. This response code is probably the most famous one due to its frequent occurrence on the web.
If you are still confused about the error, please tell me in a comment.
JavaScript - How to handle 404 error in fetch?
you can check for response status to handle 404 or other errors
use below example:
fetch('your url goes here')
.then(response => {
if (response.ok) {
return response.json()
} else if(response.status === 404) {
return Promise.reject('error 404')
} else {
return Promise.reject('some other error: ' + response.status)
}
})
.then(data => console.log('data is', data))
.catch(error => console.log('error is', error));
Related Topics
JavaScript Add Method to Object
What Are Some Empirical Technical Reasons Not to Use Jquery
How to Keep an JavaScript Object/Array Ordered While Also Maintaining Key Lookups
Remove HTML Comments with Regex, in JavaScript
Why Are Exceptions Used for Rejecting Promises in Js
Javascript: Unicode String to Hex
Remove Whitespace and Line Breaks Between HTML Elements Using Jquery
Start HTML5 Video at a Particular Position When Loading
What Is Firebase Firestore 'Reference' Data Type Good For
How to Get Size of an Element/List in Firebase Without Get It All
How to Catch Jquery $.Getjson (Or $.Ajax with Datatype Set to 'JSONp') Error When Using JSONp
How to Concatenate Regex Literals in JavaScript
Jquery: Change Event to Input File on Ie
How to Validate Google Recaptcha V2 Using JavaScript/Jquery
How to Pass Data from Child to Parent Component Angular
JavaScript - Telling Setinterval to Only Fire X Amount of Times