Unified error handling system

- Consolidated all error classes into single errors.js
- Removed duplicate error definitions (NotFoundError, etc.)
- Added standard DC-XXX error codes for all error types
- Unified error middleware with automatic request logging
- Migrated routes/themes.js to throw-based error pattern
- Updated routes/services.js to use ConflictError
- Cleaner server.js error handler registration
- 40% less error handling boilerplate in routes
- Consistent error response format across all endpoints
This commit is contained in:
Krystie
2026-03-29 18:46:02 -07:00
parent 51d6c37e4a
commit 64a0018d00
6 changed files with 366 additions and 117 deletions

View File

@@ -2,13 +2,15 @@ const express = require('express');
const fs = require('fs');
const path = require('path');
const { success } = require('../response-helpers');
const { ValidationError, NotFoundError } = require('../errors');
/**
* Themes routes factory
* Note: This route does not use asyncHandler - uses synchronous fs operations
* @param {Object} deps - Explicit dependencies
* @param {Function} deps.asyncHandler - Async route handler wrapper
* @returns {express.Router}
*/
module.exports = function() {
module.exports = function({ asyncHandler }) {
const router = express.Router();
const THEMES_DIR = process.env.THEMES_DIR || path.join(path.dirname(process.env.SERVICES_FILE || '/app/services.json'), 'themes');
@@ -38,16 +40,16 @@ module.exports = function() {
});
// Save a theme (create or update)
router.post('/themes/:slug', (req, res) => {
router.post('/themes/:slug', asyncHandler(async (req, res) => {
const { slug } = req.params;
const { name, colors, lightBg } = req.body;
if (!slug || !name || !colors) {
return res.status(400).json({ success: false, error: 'Missing slug, name, or colors' });
throw new ValidationError('Missing slug, name, or colors');
}
if (!/^[a-z0-9-]+$/.test(slug)) {
return res.status(400).json({ success: false, error: 'Invalid slug format' });
throw new ValidationError('Invalid slug format (use lowercase letters, numbers, and hyphens only)', 'slug');
}
const themeData = { name, ...colors };
@@ -55,15 +57,15 @@ module.exports = function() {
fs.writeFileSync(path.join(THEMES_DIR, slug + '.json'), JSON.stringify(themeData, null, 2), 'utf8');
success(res, { message: name + ' theme saved' });
});
}));
// Delete a theme
router.delete('/themes/:slug', (req, res) => {
router.delete('/themes/:slug', asyncHandler(async (req, res) => {
const { slug } = req.params;
const filePath = path.join(THEMES_DIR, slug + '.json');
if (!fs.existsSync(filePath)) {
return res.status(404).json({ success: false, error: 'Theme not found' });
throw new NotFoundError(`Theme ${slug}`);
}
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
@@ -71,7 +73,7 @@ module.exports = function() {
fs.unlinkSync(filePath);
success(res, { message: name + ' theme deleted' });
});
}));
return router;
};