Day 20 of 100: Error Handling — try/catch, finally, Custom Errors
Day 20 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: JavaScript Fundamentals. 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.
Tests fail. Networks drop. Good error handling separates clear failures from mystery stack traces.
Contents
try / catch / finally
async function fetchUser(id) {
try {
const res = await fakeApi(id);
return res;
} catch (err) {
console.error('fetchUser failed: ' + err.message);
throw err; // re-throw — do not swallow!
} finally {
console.log('cleanup runs no matter what');
}
}
🚀 Level Up Your Playwright
From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.
Custom Error Classes
class TestDataError extends Error {
constructor(message, entity) {
super(message);
this.name = 'TestDataError';
this.entity = entity;
}
}
function createUser(data) {
if (!data.email) {
throw new TestDataError('email required', 'user');
}
}
try { createUser({}); }
catch (e) {
if (e instanceof TestDataError) console.log('data problem: ' + e.message);
else throw e; // unknown errors bubble up
}
Rules for Test Code
- Never catch-and-ignore — silent failures are worse than crashes
- Catch only where you can act (retry, cleanup, better message)
- Let Playwright assertions throw naturally — the runner formats them
- finally is for cleanup: delete test data, close connections
Tomorrow (Day 21): JSON — parsing and validating API payloads.
🎓 Master Playwright End to End
Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.
