Skip to content
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 API's Error Handling #29

Open
Joldnine opened this issue Oct 19, 2018 · 0 comments
Open

Fetch API's Error Handling #29

Joldnine opened this issue Oct 19, 2018 · 0 comments

Comments

@Joldnine
Copy link
Owner

Joldnine commented Oct 19, 2018

Error handling of Fetch API will be much different from the way of ajax.

Normally, when the back end returns a non 200 response, the front end may either deal with the response's statusText or the response's body.

Here are the snapshots (in our example, the response body is json):

  1. response.statusText
fetch(query).then((response) => {
  if (!response.ok) {
    throw Error(response.statusText);
  }
  response.json().then((response) => {
    console.log(response)
  })
}).catch((error) => {
  // caution: error (which is response.statusText) is a ByteString, so we may need to convert it to string by error.toString()
  console.log(error.toString())
})
  1. response.body
fetch(query).then((response) => {
  if (!response.ok) {
    response.json().then((error) => {
      throw Error(error);
    }).catch(error => {
      console.log(error.message)
    })
  } else {
    response.json().then((response) => {
      console.log(response)
    })
  }
})  
@Joldnine Joldnine changed the title fetch error handling Fetch Error Handling Snapshot Nov 27, 2018
@Joldnine Joldnine changed the title Fetch Error Handling Snapshot Fetch API's Error Handling Snapshot Dec 21, 2018
@Joldnine Joldnine changed the title Fetch API's Error Handling Snapshot Fetch API's Error Handling Dec 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant