Browser Fetch API Errors — Network Failure, AbortError & TypeError
Warningweb development
Overview
Fix Fetch API errors including TypeError (network failure), AbortError (request cancelled), CORS rejection, and handling failed HTTP responses in JavaScript.
Key Details
- Fetch API throws TypeError for network failures (no connection, DNS failure, CORS block)
- Important: fetch() does NOT reject on HTTP error status codes (404, 500) — only on network errors
- You must check response.ok or response.status manually for HTTP error handling
- AbortError is thrown when a request is cancelled via AbortController
- Fetch does not support timeout natively — use AbortController with setTimeout
Common Causes
- TypeError: network failure, DNS resolution failed, or CORS blocked the request
- AbortError: request was cancelled by AbortController (timeout or user navigation)
- Unexpected HTTP errors not caught because fetch() resolved with error status
- Mixed content: fetch to HTTP URL from HTTPS page
- Request blocked by Content Security Policy connect-src directive
Steps
- 1Always check response.ok: if (!response.ok) throw new Error(`HTTP ${response.status}`)
- 2Wrap fetch in try/catch for network errors: try { await fetch(url) } catch (e) { /* network error */ }
- 3Add timeout: const controller = new AbortController(); setTimeout(() => controller.abort(), 5000); fetch(url, { signal: controller.signal })
- 4For CORS: ensure the server sends proper Access-Control-Allow-Origin headers
- 5Check DevTools > Network tab for the actual HTTP status and response details
Tags
browserfetch-apinetworkjavascripterror-handling
More in Web Development
browser-cors-error-explainedBrowser CORS Error Explained — Cross-Origin Request Blocked
Warningbrowser-websocket-errorsBrowser WebSocket Errors — Connection Failed, Closed & Protocol Errors
Warningbrowser-indexeddb-errorsBrowser IndexedDB Errors — Quota Exceeded, Blocked & Corruption
Warningbrowser-localstorage-quota-exceededBrowser localStorage Quota Exceeded — Storage Limit & Alternatives
Warningbrowser-service-worker-errorsBrowser Service Worker Errors — Registration, Cache & Update Failures
Warningbrowser-webgl-context-lostBrowser WebGL Context Lost — GPU Rendering Failure in Browser
WarningFrequently Asked Questions
By design. fetch() only rejects on network failures. HTTP errors are valid responses. You must check response.ok or response.status explicitly.