Skip to content

Instantly share code, notes, and snippets.

@vanaf1979
Created February 8, 2026 13:55
Show Gist options
  • Select an option

  • Save vanaf1979/50ab165973cf0d87d80c3cbe45f7923a to your computer and use it in GitHub Desktop.

Select an option

Save vanaf1979/50ab165973cf0d87d80c3cbe45f7923a to your computer and use it in GitHub Desktop.
Fetch errors
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