Does Fetch API throw errors when a request fails? Alternatively: How does Fetch API throw errors, and

18 Replies, 881 Views

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)

Messages In This Thread
Does Fetch API throw errors when a request fails? Alternatively: How does Fetch API throw errors, and - by - 13-04-2024, 10:20 PM



Users browsing this thread: 1 Guest(s)