QuickSnip
Browse
Tags
+ New
← Back to snippet
Edit snippet
Title
Description
(optional)
Fetch JSON with explicit error handling for non-OK responses.
Language
JavaScript
TypeScript
JSX
TSX
Python
Go
Rust
Bash
SQL
CSS
HTML
JSON
YAML
Markdown
Tags
Loading tags…
Code
export async function getJSON<T>(url: string): Promise<T> { const res = await fetch(url); if (!res.ok) { throw new Error(`Request failed: ${res.status} ${res.statusText}`); } return (await res.json()) as T; }
Update snippet
Cancel