try..catch not catching async/await errors

Perhaps I misunderstood how catching errors with async/await is supposed to work from things articles like this and this, but my catch block is not catching 400/500.

async () => {
  let response
  try {
   let response = await fetch('not-a-real-url')
  catch (err) {
    // not jumping in here.

example on codepen if it helps



400/500 is not an error, it's a response. You'd only get an exception (rejection) when there's a network problem.

When the server answers, you have to check whether it's good or not:

try {
    let response = await fetch('not-a-real-url')
    if (!response.ok) // or check for response.status
        throw new Error(response.statusText);
    let body = await response.text(); // or .json() or whatever
    // process body
} catch (err) {


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.