dev-error-handlinglisted
Install: claude install-skill christopherlouet/claude-base
# Error Handling
## Principles
1. **Fail fast** - Detect errors early
2. **Fail loud** - Log clearly
3. **Fail gracefully** - Clean UX
4. **Recover when possible** - Retry, fallback
## Custom errors
```typescript
// Base error
class AppError extends Error {
constructor(
message: string,
public code: string,
public statusCode: number = 500
) {
super(message);
this.name = this.constructor.name;
}
}
// Specific errors
class NotFoundError extends AppError {
constructor(resource: string, id: string) {
super(`${resource} not found: ${id}`, 'NOT_FOUND', 404);
}
}
class ValidationError extends AppError {
constructor(public errors: Record<string, string>) {
super('Validation failed', 'VALIDATION_ERROR', 400);
}
}
```
## API Error Response
```typescript
// Error handler middleware
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
if (err instanceof AppError) {
return res.status(err.statusCode).json({
error: {
code: err.code,
message: err.message,
}
});
}
// Log unexpected errors
logger.error({ err, requestId: req.id }, 'Unexpected error');
res.status(500).json({
error: {
code: 'INTERNAL_ERROR',
message: 'Something went wrong',
}
});
});
```
## React Error Boundary
```tsx
class ErrorBoundary extends Component<Props, State> {
state = { hasError: false, error: null };
static getDerivedStateFromError(error: Error) {
return { hasError: t