Fetch API Errors: Why a 404 is a 'Success'

A `fetch()` call only fails on network errors, not on HTTP errors like 404. You must check the `response.ok` property to see if the request was successful. The footgun is assuming a `catch` block will handle a 404; it won't.
A `fetch()` promise resolves successfully even if the server returns an error like 404 Not Found or 500 Internal Server Error; it only rejects on network-level failures. To handle HTTP errors, you must explicitly check the `response.ok` boolean property after the promise resolves. The common footgun is assuming a `catch` block will handle a 404; it won't. You must `throw` an error yourself if `response.ok` is false to correctly route logic to your error handler.
Read the original → developer.mozilla.org
- #web apis
- #javascript
- #error handling
- #fetch
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.