Created
February 8, 2026 13:55
-
-
Save vanaf1979/50ab165973cf0d87d80c3cbe45f7923a to your computer and use it in GitHub Desktop.
Fetch errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const getSecureData = async (url) => { | |
| try { | |
| const response = await fetch(url); | |
| // This is the missing piece! | |
| // If the server returns 404 or 500, we jump to the catch block manually. | |
| if (!response.ok) { | |
| throw new Error(`HTTP error! status: ${response.status}`); | |
| } | |
| const data = await response.json(); | |
| return data; | |
| } catch (error) { | |
| // This catches BOTH network failures AND our manual HTTP error above | |
| console.error("Fetch failed:", error.message); | |
| } | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment