Part 5: Error Handling and Validation

The Production Incident

At 2 AM, my phone buzzed—production alert. Our payment API was down. I opened CloudWatch logs and saw:

TypeError: Cannot read property 'amount' of undefined
TypeError: Cannot read property 'amount' of undefined
TypeError: Cannot read property 'amount' of undefined
...500 errors in 2 minutes

The bug? A client sent malformed JSON. My code assumed the amount field existed. No validation, no null checks, no error handling. The entire payment service crashed.

After 30 minutes of scrambling, I added validation and proper error handling. The service restarted. Incident resolved.

That night taught me: validation and error handling aren't optional—they're what separates hobby projects from production systems.

This article shares the error handling patterns that have kept my APIs running 24/7.

Input Validation

Why Validate?

Security: Prevent SQL injection, XSS attacks Stability: Prevent crashes from unexpected input User experience: Clear error messages Data integrity: Ensure database consistency

Validation Libraries

I use Zod for TypeScript—runtime validation with type inference.

Schema Definition

Validation Middleware

Using Validation in Routes

Error Classification

Custom Error Classes

Using Custom Errors

Global Error Handler

Using Error Handler

Rate Limiting

Prevent abuse and DOS attacks.

Request Sanitization

Prevent XSS and injection attacks.

Response Formatting

Consistent error responses across the API.

Production Error Logging

Key Takeaways

  1. Always validate input using schemas (Zod, Joi, etc.)

  2. Use custom error classes for different error types

  3. Global error handler catches all errors consistently

  4. Rate limiting prevents abuse

  5. Sanitize input to prevent XSS and injection attacks

  6. Log errors with context for debugging

  7. Hide error details in production

  8. 422 for validation errors, 400 for bad requests

  9. Async error handling with try/catch or catchAsync wrapper

  10. Consistent error responses using ResponseFormatter

Next in series: API Documentation and Versioning—making your APIs discoverable and maintainable.


Good error handling turns crashes into graceful failures. Invest time upfront to save hours of debugging later.

Last updated