Перейти до основного вмісту
← ⚡ JavaScript & the browser·Модуль C7 · Урок 10
ЗавданняDefine HttpError extends Error (with .status field) and NotFoundError extends HttpError. Write async fetchJson(url) that throws NotFoundError on status 404 and HttpError on any other not-ok. For each id in [1, 999], try fetchJson('/api/users/' + id) — success path logs user.name; instanceof NotFoundError → log 'not found'.

Capstone: typed-error fetch wrapper

250 XP14 min
Теорія

The shape every real codebase needs

A fetch wrapper that turns HTTP statuses into typed errors. Every call site gets clean narrowing: 404 → null, anything else not-OK → throw.

class HttpError extends Error {
  constructor(status, statusText) {
    super(`HTTP ${status} ${statusText}`);
    this.name = "HttpError";
    this.status = status;
  }
}
class NotFoundError extends HttpError {
  constructor(s, t) { super(s, t); this.name = "NotFoundError"; }
}

async function fetchJson(url) {
  const r = await fetch(url);
  if (r.status === 404) throw new NotFoundError(r.status, r.statusText);
  if (!r.ok)            throw new HttpError(r.status, r.statusText);
  return r.json();
}

Calling it

try {
  const user = await fetchJson("/api/users/1");
  console.log(user.name);
} catch (err) {
  if (err instanceof NotFoundError) return null;
  throw err;  // unknown — let it bubble
}

Why this scales

The pattern extends trivially: class RateLimitError, class BadRequestError, class UnauthorizedError — same five-line shape, each catches its own status. Every API client in your codebase reads the same way.

🔒

Зареєструйся щоб кодувати

Теорія відкрита всім. Інтерактивний редактор, живий перегляд і перевірка відкриваються з 7-денним безкоштовним trial — потрібна картка, скасування у будь-який час.

Реєстрація — безкоштовний trial →

Перші 10 уроків у кожному треку — безкоштовні. Картка не потрібна.

← НазадНаступний урок →

Отримуй одну пораду з Python чи вебу — щодня поштою

Коротко, без води, без спаму. Відписка — в один клік.