Implement comprehensive error handling with custom errors, logging, and graceful recovery.
You are a Node.js error handling expert. Help me implement robust error handling.
## Application Context
App type: ${{APP_TYPE}}
Error sources: ${{ERROR_SOURCES}}
Logging service: ${{LOGGING}}
Alert requirements: ${{ALERTS}}
## Please Implement:
1. **Custom Error Classes**
```typescript
// Error hierarchy:
// - AppError base class
// - ValidationError
// - AuthenticationError
// - NotFoundError
// - DatabaseError
```
2. **Async Error Handling**
```typescript
// Promise handling:
// - asyncHandler wrapper
// - Unhandled rejection
// - Async/await patterns
```
3. **Express Error Middleware**
```typescript
// Centralized handling:
// - Error formatting
// - Status mapping
// - Client response
// - Error logging
```
4. **Validation Errors**
```typescript
// Input validation:
// - Field errors
// - Error aggregation
// - User-friendly messages
```
5. **Database Errors**
```typescript
// DB error handling:
// - Connection errors
// - Query errors
// - Constraint violations
// - Retry logic
```
6. **External Service Errors**
```typescript
// Third-party failures:
// - Timeout handling
// - Circuit breaker
// - Fallback responses
// - Retry strategies
```
7. **Logging Strategy**
```typescript
// Error logging:
// - Structured logging
// - Log levels
// - Context attachment
// - Stack traces
```
8. **Monitoring & Alerts**
```typescript
// Error tracking:
// - Sentry integration
// - Alert thresholds
// - Error grouping
// - Dashboard metrics
```Or press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[{APP_TYPE][{ERROR_SOURCES][{LOGGING][{ALERTS]