ideas-generator/server/middleware/errorHandler.js
DJP 88d18619bb Complete migration from OpenAI Assistants API to Chat Completions API with Vue.js frontend
Major Features Implemented:
- Full Vue.js 3 admin interface with Vite build system
- OpenAI Chat Completions API integration (replaced deprecated Assistants API)
- PostgreSQL database with Sequelize ORM
- Complete conversation management system
- User authentication system (admin@oliver.agency, user@oliver.agency)
- AI-powered conversation title generation
- Server-Sent Events for streaming responses
- Conversation soft delete functionality
- Rate limiting middleware with development bypass

Backend Infrastructure:
- Node.js/Express server with comprehensive error handling
- Database models: User, Assistant, Conversation, Message
- Chat API endpoints with full conversation history context
- Conversation CRUD operations with soft delete
- Migration and seeding scripts
- Environment-based configuration

Frontend Features:
- Responsive Vue.js interface with router
- Real-time chat with streaming responses
- Conversation sidebar with delete functionality
- Agent selection dropdown
- Persistent user sessions with hash-based user IDs
- Conversation history loading and continuity
- Login system with user role management
- Prominent logout functionality

Technical Improvements:
- Fixed conversation continuity by loading full message history
- Implemented conversation title generation using GPT-4o-mini
- Added conversation persistence mechanisms (periodic refresh, window focus)
- Enhanced error handling and rate limiting
- Proper environment variable management
- Clean project structure with separated concerns

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-03 13:08:26 -04:00

55 lines
No EOL
1.6 KiB
JavaScript

const errorHandler = (err, req, res, next) => {
console.error('Error occurred:', {
message: err.message,
stack: process.env.NODE_ENV === 'development' ? err.stack : undefined,
path: req.path,
method: req.method,
timestamp: new Date().toISOString(),
});
if (err.name === 'ValidationError') {
return res.status(400).json({
error: 'Validation Error',
message: err.message,
details: err.details || {},
});
}
if (err.name === 'SequelizeValidationError') {
return res.status(400).json({
error: 'Database Validation Error',
message: 'Invalid data provided',
details: err.errors?.map(e => ({ field: e.path, message: e.message })) || [],
});
}
if (err.name === 'SequelizeUniqueConstraintError') {
return res.status(409).json({
error: 'Conflict',
message: 'Resource already exists',
details: err.errors?.map(e => ({ field: e.path, message: e.message })) || [],
});
}
if (err.status || err.statusCode) {
return res.status(err.status || err.statusCode).json({
error: err.name || 'Error',
message: err.message,
});
}
if (err.code === 'OPENAI_API_ERROR') {
return res.status(502).json({
error: 'OpenAI Service Error',
message: 'There was an issue communicating with OpenAI. Please try again.',
});
}
res.status(500).json({
error: 'Internal Server Error',
message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong!',
...(process.env.NODE_ENV === 'development' && { stack: err.stack }),
});
};
module.exports = errorHandler;