CodShot
JavaScript Public

Fetch JSON with Error Handling

Fetch JSON data and handle HTTP errors cleanly.

#javascript #fetch #json #error-handling
JavaScript
async function loadProfile() {
    const response = await fetch('/api/profile');

    if (!response.ok) {
        throw new Error(`Request failed: ${response.status}`);
    }

    return await response.json();
}

loadProfile()
    .then(profile => console.log(profile))
    .catch(error => console.error(error.message));

Notes

fetch only rejects on network errors, so check response.ok for HTTP errors.

Snippet actions

CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support