errors
Error Handling Patterns
Robust error handling for production applications.
Instructions
1. Custom Error Classes
class AppError extends Error {
constructor(
public message: string,
public statusCode: number = 500,
public code?: string
) {
super(message);
this.name = 'AppError';
Error.captureStackTrace(this, this.constructor);
}
}
class NotFoundError extends AppError {
constructor(resource: string) {
super(`${resource} not found`, 404, 'NOT_FOUND');
}
}
class ValidationError extends AppError {
constructor(message: string) {
super(message, 400, 'VALIDATION_ERROR');
}
}
2. Async Error Handling
// ✅ Async/await with try-catch
async function fetchUser(id: string) {
try {
const response = await api.get(`/users/${id}`);
return response.data;
} catch (error) {
if (error instanceof AxiosError) {
throw new AppError(error.message, error.response?.status);
}
throw error;
}
}
// ✅ Promise error handling
fetchUser(id)
.then(handleSuccess)
.catch(handleError);
3. Express Error Middleware
// Error handler middleware
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error(err.stack);
if (err instanceof AppError) {
return res.status(err.statusCode).json({
success: false,
error: {
message: err.message,
code: err.code
}
});
}
res.status(500).json({
success: false,
error: {
message: 'Internal server error',
code: 'INTERNAL_ERROR'
}
});
});
4. React Error Boundaries
class ErrorBoundary extends React.Component<Props, State> {
state = { hasError: false, error: null };
static getDerivedStateFromError(error: Error) {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('Error caught:', error, errorInfo);
// Log to error reporting service
}
render() {
if (this.state.hasError) {
return <ErrorFallback error={this.state.error} />;
}
return this.props.children;
}
}
5. API Error Response Format
// Standard error response
interface ApiError {
success: false;
error: {
message: string;
code: string;
details?: unknown;
};
}
// Example response
{
"success": false,
"error": {
"message": "User not found",
"code": "USER_NOT_FOUND"
}
}
6. Logging
import winston from 'winston';
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
// Usage
logger.error('Database connection failed', { error, context });
References
More from alicoder001/agent-skills
react-nextjs
Next.js 14+ App Router best practices including Server Components, data fetching, caching, and Vercel optimization patterns. Use for Next.js development with waterfall prevention, bundle optimization, and server actions.
17javascript
JavaScript micro-optimizations and performance patterns. Use when optimizing loops, array operations, caching, or DOM manipulation. Includes Set/Map usage, early returns, and memory-efficient patterns.
14enterprise-ddd
Enterprise-grade architecture combining DDD bounded contexts with Feature-Sliced Design. Use for large-scale monorepos with multiple domains, microservices, event-driven communication, and scalable frontend modules.
14global-config
Global defaults and interaction rules for all Alicoder001 skills. Use when starting any task to set communication style, project assumptions, and skill priority.
12senior
Senior frontend developer practices including code quality, accessibility, responsive design, and UI consistency. Use when reviewing code, implementing UX patterns, or ensuring production-quality output.
12workflow
AI agent operational rules including token discipline, navigation-first approach, and output contracts. Use when you need efficient and predictable agent behavior during development tasks.
10