-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fetch should throw an error when it receives a 404 #155
Comments
You would think so, but that's not per spec. The spec only rejects the promise if there was an error making or receiving the response. A HTTP 404 should be treated as a "successful" response and is up to the user to decide what to do with such response. If you want to reject non-20x responses, you can make your own wrapper: function myFetch(url, options) {
if (options == null) options = {}
if (options.credentials == null) options.credentials = 'same-origin'
return fetch(url, options).then(function(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response)
} else {
var error = new Error(response.statusText || response.status)
error.response = response
return Promise.reject(error)
}
})
} This resembles XMLHttpRequest in its handling of same-domain cookies and successful responses. |
Actually, in this specific case, I want to accept only status 200, so having my own code "if (response.status === 200) ..." instead of "if (response.status >= 200 && response.status < 300) ..." is handy. Thanks for the wrapper code, but for my project, the following is sufficient AFAICS:
(Inside a Promise wrapper containing additional stuff:)
Also: related: whatwg/fetch#60 . |
When there's eg a 404, fetch should throw an error, so that I can .catch() and handle it. (The error related code in the readme should be in fetch by default.)
The text was updated successfully, but these errors were encountered: