{"success":true,"name":"fetch-doesn-t-throw-on-http-errors-check-response-ok-yoursel","description":"Using fetch in JavaScript and handling failures with try/catch","category":"ai-brimind","path":"ai-brimind/fetch-doesn-t-throw-on-http-errors-check-response-ok-yoursel/SKILL.md","file":"SKILL.md","content":"---\nname: fetch-doesn-t-throw-on-http-errors-check-response-ok-yoursel\ndescription: \"Using fetch in JavaScript and handling failures with try/catch\"\n---\n\n# fetch Doesn't Throw on HTTP Errors: Check response.ok Yourself\n\n## When to use\nProject is JavaScript, and code assumes a catch block around fetch will handle 404s and 500s, or parses the response body without checking status.\n\n## Example request\nserver returns 500, catch never fires → fetch only rejects on network failure, not HTTP errors; the trap itself. response.json() called on an error page → crash or garbage data parsed as success; unchecked body. a 404 flows into the success path → empty screen with no error shown; failure dressed as data. — each trusted catch to see what fetch never throws.\n\n## Instructions\nAct as a mail clerk who opens every envelope to check the letter, knowing delivery alone proves nothing. In JavaScript, fetch rejects only when the request itself couldn't happen — network down, DNS failure, abort; a server answering 404, 401, or 500 is a successful fetch, so your catch block never fires and the error sails into the success path. Therefore, after every fetch, check response.ok (or the specific status) before touching the body, and route failures explicitly: a not-ok response is an error case with its own handling, its own message, and the status and body captured for the log. Never call response.json() before that check — error pages are often not JSON, and the parse crash will mask the real failure. Handle both failure families distinctly: network errors (caught) and HTTP errors (checked), because 'server said no' and 'server unreachable' deserve different messages. If the project has one shared API client, this check belongs there once, not copy-pasted per call. Only proceed once no response body is ever consumed before its status is verified.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #734\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}