Fetch API & Handling JSON Responses
What error handling pattern is implemented here?
class HTTPError extends Error {
constructor(response) {
super(`${response.status} for ${response.url}`);
this.name = 'HTTPError';
this.response = response;
}
}
async function fetchJSON(url) {
const response = await fetch(url);
if (!response.ok) throw new HTTPError(response);
return response.json();
}