ExamplesError Handling

Error Handling Patterns

Comprehensive guide to mocking error scenarios and testing error handling.

For complete examples with detailed code, see the Error Handling Patterns section in MASTER_DOCUMENTATION.md.


Client Errors (4xx)

400 Bad Request

const badRequestRecording = createRecording(
  { method: 'POST', path: '/users', body: { email: 'invalid' }, timestamp: Date.now() },
  {
    status: 400,
    body: {
      error: 'Bad Request',
      errors: [{ field: 'email', message: 'Invalid email format' }]
    },
    timestamp: Date.now()
  }
)

404 Not Found

const notFoundRecording = createRecording(
  { method: 'GET', path: '/users/999', timestamp: Date.now() },
  {
    status: 404,
    body: { error: 'Not Found', message: 'User not found' },
    timestamp: Date.now()
  }
)

Server Errors (5xx)

500 Internal Server Error

const serverErrorRecording = createRecording(
  { method: 'GET', path: '/data', timestamp: Date.now() },
  {
    status: 500,
    body: { error: 'Internal Server Error', errorId: 'err_123' },
    timestamp: Date.now()
  }
)

Full Documentation

See MASTER_DOCUMENTATION.md for complete error handling examples.