最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to catch the status code when fetch fails using async await - Stack Overflow

programmeradmin0浏览0评论

Context

I am going from using Promises to async/await for one of my fetch request (which I use inside a Vuex action, but this is not necessary to understand my issue).

Using the code below, I am able to provide the end user an error message depending the status code of my response in case the request failed.

fetch("http://localhost:8000/api/v1/book", {
  headers: {
    Accept: "application/json"
  }
}).then(response => {
  if (!response.ok) {
    if (response.status === 429) {
      // displaying "wow, slow down mate"
    } else if (response.status === 403) {
      // displaying "hm, what about no?"
    } else {
      // displaying "dunno what happened \_(ツ)_/¯"   
    }

    throw new Error(response);
  } else {
    return response.json();
  }
}).then(books => {
  // storing my books in my Vuex store
})
.catch(error => {
  // storing my error onto Sentry
});

Issue

Using async/await, this is what my code looks like now:

try {
  const response = await fetch("http://localhost:8000/api/v1/book", {
    headers: {
      Accept: "application/json"
    }
  });

  const books = await response.json();

  // storing my books
} catch(exception) {
  // storing my error onto Sentry
}

Question

How can I figure out which status code my response returned in case it failed using async/await?

If I am using this in the wrong way, just do not hesitate to correct me with a better pattern.

Notes

I have made a JSFiddle to test the issue live. Feel free to update it.

/

Context

I am going from using Promises to async/await for one of my fetch request (which I use inside a Vuex action, but this is not necessary to understand my issue).

Using the code below, I am able to provide the end user an error message depending the status code of my response in case the request failed.

fetch("http://localhost:8000/api/v1/book", {
  headers: {
    Accept: "application/json"
  }
}).then(response => {
  if (!response.ok) {
    if (response.status === 429) {
      // displaying "wow, slow down mate"
    } else if (response.status === 403) {
      // displaying "hm, what about no?"
    } else {
      // displaying "dunno what happened \_(ツ)_/¯"   
    }

    throw new Error(response);
  } else {
    return response.json();
  }
}).then(books => {
  // storing my books in my Vuex store
})
.catch(error => {
  // storing my error onto Sentry
});

Issue

Using async/await, this is what my code looks like now:

try {
  const response = await fetch("http://localhost:8000/api/v1/book", {
    headers: {
      Accept: "application/json"
    }
  });

  const books = await response.json();

  // storing my books
} catch(exception) {
  // storing my error onto Sentry
}

Question

How can I figure out which status code my response returned in case it failed using async/await?

If I am using this in the wrong way, just do not hesitate to correct me with a better pattern.

Notes

I have made a JSFiddle to test the issue live. Feel free to update it.

https://jsfiddle/180ruamk/

Share Improve this question edited Jul 10, 2019 at 18:07 Anwar asked Jul 10, 2019 at 16:18 AnwarAnwar 4,2565 gold badges43 silver badges67 bronze badges 3
  • 1 Exactly the same when you wasn't using async / await.. response.status etc. – Keith Commented Jul 10, 2019 at 16:22
  • 1 But to be the same as your thenable, your const books wants to be inside your catch.. – Keith Commented Jul 10, 2019 at 16:23
  • 1 I tried but I cannot access my response.status right after the await fetch() because it goes right into the catch(exception) block, and the exception misses the response data. – Anwar Commented Jul 10, 2019 at 16:27
Add a ment  | 

1 Answer 1

Reset to default 2

It'll be exactly the same code as in your then callback:

try {
  const response = await fetch("http://localhost:8000/api/v1/book", {
    headers: {
      Accept: "application/json"
    }
  });
  if (!response.ok) {
    if (response.status === 429) {
      // displaying "wow, slow down mate"
    } else if (response.status === 403) {
      // displaying "hm, what about no?"
    } else {
      // displaying "dunno what happened \_(ツ)_/¯"   
    }
    throw new Error(response);
  }
  const books = await response.json();

  // storing my books
} catch(exception) {
  // storing my error onto Sentry
}
发布评论

评论列表(0)

  1. 暂无评论