✅ Testing & Verification

API Error Handling

Project Evidence Required
Try/catch blocks for API calls, network error handling.
Assessment Method
Code review: Error handling for fetch failures

What it is

Error handling is the defensive code that catches failures and recovers gracefully. In JavaScript, try/catch wraps risky code; .catch() handles rejected Promises; if (!response.ok) checks for HTTP error statuses (since fetch only rejects on network failures, not 4xx/5xx responses).

Why it matters

Networks fail. APIs go down. Wifi cuts out. Without error handling, any of these crashes the game. With error handling, the game shows a friendly message and keeps running.

How GateGame implements it

Every fetch is wrapped in try/catch. On error, the leaderboard shows ‘Couldn’t connect — try again later’ instead of throwing. The NPC AI falls back to a canned response if the request times out. I tested this by killing wifi mid-fetch and confirming the game stayed playable.

Code Example

try {
  const response = await fetch('/api/leaderboard', {
    method: 'POST',
    body: JSON.stringify({ score, player })
  });
  if (!response.ok) {
    throw new Error(`HTTP ${response.status}`);
  }
  const data = await response.json();
  renderLeaderboard(data);
} catch (error) {
  console.error('Leaderboard API error:', error);
  showOfflineFallback();  // ← game stays playable
}

Key Takeaway

Every fetch goes inside try/catch. Plan for failure — the network will let you down eventually.