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

Closed
tobireif opened this Issue Jun 3, 2015 · 2 comments

Comments

Projects
None yet
2 participants
@tobireif

tobireif commented Jun 3, 2015

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.)

@mislav

This comment has been minimized.

Show comment
Hide comment
@mislav

mislav Jun 3, 2015

Member

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.

Member

mislav commented Jun 3, 2015

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.

@mislav mislav closed this Jun 3, 2015

@tobireif

This comment has been minimized.

Show comment
Hide comment
@tobireif

tobireif Jun 3, 2015

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:

function fetchStatusHandler(response) {
  if (response.status === 200) {
    return response;
  } else {
    throw new Error(response.statusText);
  }
}

(Inside a Promise wrapper containing additional stuff:)

fetch(url)
  .then(
    fetchStatusHandler
  ).then(function(response) {
    return response.blob();
  }).then(function(blob) {
    resolve(blob.size);
  }).catch(function(error) {
    reject(error);
  });

Also: related: whatwg/fetch#60 .

tobireif commented Jun 3, 2015

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:

function fetchStatusHandler(response) {
  if (response.status === 200) {
    return response;
  } else {
    throw new Error(response.statusText);
  }
}

(Inside a Promise wrapper containing additional stuff:)

fetch(url)
  .then(
    fetchStatusHandler
  ).then(function(response) {
    return response.blob();
  }).then(function(blob) {
    resolve(blob.size);
  }).catch(function(error) {
    reject(error);
  });

Also: related: whatwg/fetch#60 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment