AI Workshop: learn to build apps with AI →
Fetch: Catching errors in network requests

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


Since fetch() returns a promise, we can use the catch method of the promise to catch any error that occurs during the request or in the then callbacks:

fetch('./file.json')
.then(response => {
  // ...
})
.catch(err => console.error(err))

Another way of catching errors is to manage them in the first then:

fetch('./file.json')
.then(response => {
  if (!response.ok) { throw Error(response.statusText) }
  return response
})
.then(response => {
  // ...
})

Lessons in this unit:

0: Introduction
1: How to use Fetch
2: ▶︎ Catching errors in network requests
3: The Response object
4: Getting the body content
5: The Request object
6: Request headers
7: POST requests