Error Handling in Async Code
What error handling best practice is shown here?
class APIError extends Error {
constructor(message, status, code) {
super(message);
this.name = 'APIError';
this.status = status;
this.code = code;
}
}
async function fetchData() {
const response = await fetch('/api');
if (!response.ok) {
throw new APIError('API request failed', response.status, 'ERR_API');
}
return response.json();
}