TechTalks
Chapter 11 of 12

11.1 Error Handling & Custom Errors

Robust applications subclass the built-in Error object to create typed domain errors.

Custom Domain Errorjavascript
class ValidationError extends Error {
  constructor(message, field) {
    super(message);
    this.name = 'ValidationError';
    this.field = field;
  }
}

try {
  throw new ValidationError('Invalid Email Format', 'email');
} catch (err) {
  if (err instanceof ValidationError) {
    console.error(`Validation failed on field ${err.field}: ${err.message}`);
  } else {
    throw err;
  }
}

11.2 Automated Unit Testing with Vitest

Vitest Unit Test Suitejavascript
import { describe, it, expect, vi } from 'vitest';

describe('Payment Processor', () => {
  it('should process payment successfully', async () => {
    const mockApi = vi.fn().mockResolvedValue({ success: true, id: 'tx_100' });
    const res = await mockApi(50);
    
    expect(mockApi).toHaveBeenCalledWith(50);
    expect(res.success).toBe(true);
  });
});