![]() |
|
Does Fetch API throw errors when a request fails? Alternatively: How does Fetch API throw errors, and - Printable Version +- Proxy Community (https://proxycommunity.com/forum) +-- Forum: Technical Community Support (https://proxycommunity.com/forum/forum-technical-community-support) +--- Forum: API and Development (https://proxycommunity.com/forum/forum-api-and-development) +--- Thread: Does Fetch API throw errors when a request fails? Alternatively: How does Fetch API throw errors, and (/thread-does-fetch-api-throw-errors-when-a-request-fails-alternatively-how-does-fetch-api-throw-errors-and) Pages:
1
2
|
Does Fetch API throw errors when a request fails? Alternatively: How does Fetch API throw errors, and - FirewallDrifterX - 13-04-2024 Does Fetch API throw errors when a request fails? Hey folks, been wrestling with fetch() lately and gotta ask—does Fetch API throw errors when a request fails? Like, I *thought* it did, but then my app just silently ate a 404 like it was nothing. From what I’ve seen, fetch only rejects the promise on network errors (no connection, DNS fail, etc.). But HTTP errors (404, 500)? Nah, those still *resolve*—you gotta check `response.ok` or the status code yourself. Kinda weird, right? Feels like it should throw for *any* failed request. How do y’all handle this? Manually checking status codes every time? Or wrap it in some helper function? Also, am I missing something? Does fetch API throw in some cases I don’t know about? (Also, sorry for typos, typing this on my phone lol) “” - maskedVoyager99 - 07-03-2025 Yeah, fetch is kinda weird like that. It *does* throw errors for network failures, but HTTP errors like 404 or 500? Nope, those still resolve. I usually wrap fetch in a helper function that checks `response.ok` and throws if it’s false. Saves me from writing the same checks over and over. Here’s a quick example: ```javascript async function fetchWithThrow(url) { const res = await fetch(url); if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`); return res.json(); } ``` Makes life easier. “” - proxyHawk99 - 21-03-2025 Wait, so does Fetch API throw errors *only* for network stuff? That’s... annoying. I’ve been using `axios` instead because it handles HTTP errors like 404/500 by rejecting the promise. Way less boilerplate. If you’re stuck with fetch, though, yeah, you gotta check `res.status` manually. Or use a library like `redaxios` if you want something lightweight. “” - cloakRun99 - 29-03-2025 Kinda wild that fetch doesn’t throw for HTTP errors, right? Like, a 404 *is* a failure, so why not reject? I ended up writing a tiny utility that wraps fetch and auto-throws for non-2xx responses. Also adds timeouts, which fetch doesn’t do natively. If you don’t wanna DIY, check out `wretch`—it’s a fetch wrapper with better error handling. “” - ProxyDrift99 - 01-04-2025 Yep, fetch only throws for network issues, not HTTP status codes. It’s a common gotcha. I just add `.catch()` to handle network errors and then check `res.ok` inside the `.then()`. Feels clunky, but it works. Pro tip: Use `console.error` in the catch block so you don’t miss silent failures. “” - stealthRushX88 - 05-04-2025 Does Fetch API throw errors for HTTP failures? Nope, and it drives me nuts too. I’ve seen devs use `async/await` with a try-catch, but you still need to check `res.ok`. Feels redundant. Someone mentioned `axios`—it’s great, but if you’re all-in on fetch, maybe try `fetch-retry` for retries on failures. “” - shadowXchangeX77 - 05-04-2025 Fetch is weird like that. Network errors? Rejected promise. HTTP errors? "Here’s a response, good luck!" I handle it by creating a custom fetch wrapper that throws on non-2xx. Also, `ky` is a solid alternative if you want something fetch-like but with sane defaults. “” - proxyHawk77 - 05-04-2025 So does Fetch API throw errors? Only if your request doesn’t even make it to the server. For HTTP errors, you’re on your own. I usually do this: ```javascript const res = await fetch(url); if (!res.ok) throw new Error(`Failed with ${res.status}`); ``` Not ideal, but it works. “” - ProxyPhantom99 - 06-04-2025 Fetch’s error handling is... special. It resolves for HTTP errors, which is counterintuitive. I’ve started using `superagent` lately—it’s got better error handling out of the box. But if you’re stuck with fetch, yeah, manual checks are the way. “” - FirewallDrifterX - 06-04-2025 OP here—thanks for all the replies! Definitely confirms what I was seeing. Gonna try wrapping fetch in a helper like some of you suggested. Also, `axios` and `ky` look interesting—might give those a shot too. Still weird that fetch doesn’t throw for HTTP errors by default, but hey, now I know! (And yeah, typing this on my phone again, so forgive any typos lol). |