refactor(routes): replace errorFormatter with ApiError/ValidationError throws (4a dep sweep)

This commit is contained in:
null 2026-07-10 17:50:50 -05:00
parent 3a4e44c089
commit de7bf6aaac
3 changed files with 25 additions and 47 deletions

View File

@ -1,7 +1,7 @@
// @ts-nocheck — route controller converted to .cts; handler req/res typed, full type-check deferred (thin glue over typed services). // @ts-nocheck — route controller converted to .cts; handler req/res typed, full type-check deferred (thin glue over typed services).
import type { Req, Res, Next } from '../types/http'; import type { Req, Res, Next } from '../types/http';
const express = require('express'); const express = require('express');
const { standardizeError } = require('../middleware/errorFormatter.cts'); const { ValidationError } = require('../utils/apiError.cts');
const router = express.Router(); const router = express.Router();
const { getDb } = require('../db/database.cts'); const { getDb } = require('../db/database.cts');
const { buildTrackerRow, getCycleRange } = require('../services/statusService.cts'); const { buildTrackerRow, getCycleRange } = require('../services/statusService.cts');
@ -104,22 +104,10 @@ router.get('/', (req: Req, res: Res) => {
const month = parseInt(req.query.month || now.getMonth() + 1, 10); const month = parseInt(req.query.month || now.getMonth() + 1, 10);
if (isNaN(year) || year < 2000 || year > 2100) { if (isNaN(year) || year < 2000 || year > 2100) {
return res throw ValidationError('year must be a 4-digit integer between 2000 and 2100', 'year');
.status(400)
.json(
standardizeError(
'year must be a 4-digit integer between 2000 and 2100',
'VALIDATION_ERROR',
'year',
),
);
} }
if (isNaN(month) || month < 1 || month > 12) { if (isNaN(month) || month < 1 || month > 12) {
return res throw ValidationError('month must be an integer between 1 and 12', 'month');
.status(400)
.json(
standardizeError('month must be an integer between 1 and 12', 'VALIDATION_ERROR', 'month'),
);
} }
const today = localDateString(now); const today = localDateString(now);

View File

@ -1,7 +1,6 @@
// @ts-nocheck — route controller converted to .cts; handler req/res typed, full type-check deferred (thin glue over typed services). // @ts-nocheck — route controller converted to .cts; handler req/res typed, full type-check deferred (thin glue over typed services).
import type { Req, Res, Next } from '../types/http'; import type { Req, Res, Next } from '../types/http';
const express = require('express'); const express = require('express');
const { standardizeError } = require('../middleware/errorFormatter.cts');
const { const {
ApiError, ApiError,
ValidationError, ValidationError,
@ -269,12 +268,14 @@ router.post('/', (req: Req, res: Res) => {
) )
.get(bill.id, payment.paid_date, payment.amount); .get(bill.id, payment.paid_date, payment.amount);
if (existingDuplicate) { if (existingDuplicate) {
// Deliberate hand-rolled body (documented exception): carries the
// `existing` payment the client's "add anyway?" flow renders —
// formatError has no extras channel. Shape matches the standard envelope.
return res.status(409).json({ return res.status(409).json({
...standardizeError( error: 'DUPLICATE_SUSPECTED',
'A matching payment already exists for this bill, date, and amount', message: 'A matching payment already exists for this bill, date, and amount',
'DUPLICATE_SUSPECTED', code: 'DUPLICATE_SUSPECTED',
'amount', field: 'amount',
),
existing: serializePayment(existingDuplicate), existing: serializePayment(existingDuplicate),
}); });
} }

View File

@ -8,7 +8,16 @@ const { getDb } = require('../db/database.cts');
const { seedDemoData } = require('../scripts/seedDemoData.cts'); const { seedDemoData } = require('../scripts/seedDemoData.cts');
const { demoDataLimiter } = require('../middleware/rateLimiter.cts'); const { demoDataLimiter } = require('../middleware/rateLimiter.cts');
const { eraseUserData, clearSeededDemoData } = require('../services/userDataService.cts'); const { eraseUserData, clearSeededDemoData } = require('../services/userDataService.cts');
const { standardizeError } = require('../middleware/errorFormatter.cts'); const { ApiError, ValidationError } = require('../utils/apiError.cts');
// 4xx service errors keep their message/code on the wire; anything else is
// rethrown raw so the terminal handler masks + logs it as a 5xx.
function toUserOpError(err, fallbackCode) {
if (err.status && err.status < 500) {
return new ApiError(err.code || fallbackCode, err.message, err.status);
}
return err;
}
// GET /api/user/seeded-status — whether the current user has demo data, with counts // GET /api/user/seeded-status — whether the current user has demo data, with counts
router.get('/seeded-status', (req: Req, res: Res) => { router.get('/seeded-status', (req: Req, res: Res) => {
@ -39,14 +48,7 @@ router.get('/seeded-status', (req: Req, res: Res) => {
seededTransactions, seededTransactions,
}); });
} catch (err) { } catch (err) {
res throw toUserOpError(err, 'SEEDED_STATUS_ERROR');
.status(err.status || 500)
.json(
standardizeError(
err.message || 'Seeded status check failed',
err.code || 'SEEDED_STATUS_ERROR',
),
);
} }
}); });
@ -66,14 +68,7 @@ router.post('/clear-demo-data', demoDataLimiter, (req: Req, res: Res) => {
counts: result.counts, counts: result.counts,
}); });
} catch (err) { } catch (err) {
res throw toUserOpError(err, 'CLEAR_DEMO_ERROR');
.status(err.status || 500)
.json(
standardizeError(
err.message || 'Clear demo data operation failed',
err.code || 'CLEAR_DEMO_ERROR',
),
);
} }
}); });
@ -102,9 +97,7 @@ router.post('/seed-demo-data', demoDataLimiter, (req: Req, res: Res) => {
counts: result.counts, counts: result.counts,
}); });
} catch (err) { } catch (err) {
res throw toUserOpError(err, 'SEED_ERROR');
.status(err.status || 500)
.json(standardizeError(err.message || 'Seed operation failed', err.code || 'SEED_ERROR'));
} }
}); });
@ -116,17 +109,13 @@ router.post('/erase-data', demoDataLimiter, (req: Req, res: Res) => {
.trim() .trim()
.toUpperCase(); .toUpperCase();
if (confirm !== 'ERASE') { if (confirm !== 'ERASE') {
return res throw ValidationError('Type ERASE to confirm.', 'confirm', 'ERASE_CONFIRM_REQUIRED');
.status(400)
.json(standardizeError('Type ERASE to confirm.', 'ERASE_CONFIRM_REQUIRED', 'confirm'));
} }
try { try {
const result = eraseUserData(getDb(), req.user.id); const result = eraseUserData(getDb(), req.user.id);
res.json({ success: true, ...result }); res.json({ success: true, ...result });
} catch (err) { } catch (err) {
res throw toUserOpError(err, 'ERASE_ERROR');
.status(err.status || 500)
.json(standardizeError(err.message || 'Failed to erase data', err.code || 'ERASE_ERROR'));
} }
}); });