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
- Making GET Requests
- POST and Sending Data
- Handling Errors and Status Codes
- Aborting Requests with AbortController