0Pricing
JavaScript Academy · Lesson

Handling Errors and Status Codes

Check response.ok and handle failures.

Fetch Does Not Reject on HTTP Errors

A surprising gotcha: fetch only rejects on network failures. A 404 or 500 response still resolves successfully. You must check the status yourself.

Checking response.ok

response.ok is true only for status codes in the 200-299 range. Use it to detect HTTP errors.

const response = await fetch(url);
if (!response.ok) {
  console.log("HTTP error:", response.status);
}

All lessons in this course

  1. Making GET Requests
  2. POST and Sending Data
  3. Handling Errors and Status Codes
  4. Aborting Requests with AbortController
← Back to JavaScript Academy