Error Handling Template

This template helps you implement robust error handling strategies that improve application reliability and user experience. It covers validation, exception handling, logging, and graceful degradation patterns.

Sample Prompt

Add comprehensive error handling to this user registration function:
- Input validation for email and password
- Exception catching for database operations
- Graceful degradation for external services
- User-friendly error messages
- Logging for debugging

[Provide your code here]

What This Template Covers

  • Input validation with comprehensive checking and sanitization
  • Exception handling with try-catch blocks and error boundaries
  • Custom error classes for different types of failures
  • Logging strategies for debugging and monitoring
  • Graceful degradation when services are unavailable
  • User-friendly messaging that doesn’t expose internal details

Best Practices

  1. Create specific error types for different failure scenarios
  2. Validate inputs early and provide clear feedback
  3. Use try-catch blocks around risky operations
  4. Log errors comprehensively for debugging and monitoring
  5. Implement retry logic for transient failures
  6. Gracefully degrade when non-critical services fail
  7. Don’t expose internal details in user-facing error messages
  8. Use error boundaries in React applications

When to Use This Template

  • Adding error handling to existing code
  • Building robust APIs and services
  • Implementing user input validation
  • Creating fault-tolerant applications
  • Setting up monitoring and logging
  • Improving application reliability

The Script

peep error-handling