|

Day 54 of 100: Typed Error Handling — unknown Catches and Error Classes

Day 54 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: TypeScript. JavaScript (Days 1-33) → TypeScript (Days 34-63) → Playwright (Days 64-96) → AI Framework Capstone (Days 97-100).

🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.


TypeScript catches are unknown by default in strict mode. Handle errors with the same rigor as data.

Contents

The unknown Catch

try {
  await fetch('https://bad-host.invalid');
} catch (e: unknown) {
  // e.message  — error: e is unknown
  if (e instanceof Error) {
    console.log('message:', e.message);
  } else {
    console.log('non-Error thrown:', String(e));
  }
}

🚀 Level Up Your Playwright

From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.

Error Message Extractor

function errMsg(e: unknown): string {
  if (e instanceof Error) return e.message;
  if (typeof e === 'string') return e;
  try { return JSON.stringify(e); } catch { return String(e); }
}
// One util, used in every catch across the framework.

Typed Error Classes for Triage

class ApiError extends Error {
  constructor(message: string, readonly status: number, readonly url: string) {
    super(message);
    this.name = 'ApiError';
  }
}

class TestDataError extends Error {
  constructor(message: string, readonly entity: string) {
    super(message);
    this.name = 'TestDataError';
  }
}

try { throw new ApiError('rate limited', 429, '/api/users'); }
catch (e: unknown) {
  if (e instanceof ApiError && e.status === 429) {
    console.log('backoff and retry', e.url);
  } else { throw e; }
}

Tomorrow (Day 55): Decorators — recognizing them, not needing them.

🎓 Master Playwright End to End

Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.