Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced20 minLESSON 26 / 34

fetch & network errors

01

Understand the idea

A missing resource returns 404, yet fetch enters your success path. HTTP failure statuses do not reject fetch by themselves.

Several checkpoints, several possible failures

A request can reach a server successfully and still return an error response. It can return a successful status with malformed or unexpected data. Separating these cases lets you show a useful message instead of a vague “something went wrong” for every failure.

Getting a response is only the first check

Fetch returns a for a Response . That object carries status information and provides to read the body. It is not the final application data, so handing it directly to a view that expects a count skips several necessary checks.

HTTP is the request/response protocol used by the web. A response includes a status number and a body. fetch normally fulfills with a Response even for statuses such as 404 or 500; it rejects for failures such as a network problem or a browser-blocked request. Check response.ok before treating the body as successful application data.

HTTP responses and the decisions they need
Status or failureWhat it meansTypical next step
200–299 (response.ok)HTTP reports successRead and validate the expected body; 204 may have no body
400The server rejected the request inputInspect the request and explain the input problem
401 / 403Authentication missing or access forbiddenHandle identity or permission; do not retry blindly
404The requested resource was not foundCheck the URL or show an appropriate empty/error state
429Too many requestsRespect retry guidance and reduce request frequency
500–599A server-side failureReport it and use bounded retries only when appropriate
Network or CORS rejectionNo readable successful fetch responseCheck connectivity or server access policy

Read the status deliberately

response.ok reports a status in the 200–299 range. Decide how to handle other statuses at the point where you know what the request was trying to do. An empty account, a missing resource, and a server outage can require different messages, even though none gives you the requested items.

Parse and inspect the body

Use try/catch around the awaited workflow at a boundary that can report a useful error. Keep the stage that failed in the message: a malformed body is a different problem from a count field containing text. Avoid turning every failure into empty data, because that hides the difference between an outage and a genuinely empty account.

Reading response.json() is another step and can fail on invalid JSON. Successful parsing still does not validate your fields. The example checks count's type; an application might also require it to be finite, non-negative, and an integer. The data URL keeps this exercise independent of a live service, while the same checkpoints apply to an HTTP response.

Handle cancellation and stale results

Cross-origin permission is another checkpoint, controlled by the server’s response headers and the browser. Switching to no-cors does not produce readable JSON. Request access from the owner or route an authorized request through your own backend when that is an appropriate part of the application.

Use an AbortController when leaving the screen or replacing a request makes the result obsolete. Cancellation has to be connected through the request's signal. Even with cancellation, decide which request is allowed to update the current screen, so a slow older response cannot overwrite newer results.

02

Mental model

FOLLOW THE VALUE

fetch
transport + status
Response

A fulfilled request may still have an unsuccessful HTTP status.

01 / 03Trace the transformation

03

Live example

The data URL makes the success path deterministic. To exercise failure, replace count’s number with a string, or replace the URL with data:application/,invalid; the catch reports the validation or parsing failure.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Wait for the body

Await JSON parsing before validating and reading count.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

3

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Does fetch reject solely because the response status is 404?
    02What does response.json return?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson