From e9f52b74724bbe1f21421e6b48106a0ad8dc9222 Mon Sep 17 00:00:00 2001 From: null Date: Fri, 10 Jul 2026 17:52:32 -0500 Subject: [PATCH] refactor(routes/user): replace errorFormatter with ApiError/ValidationError throws --- routes/user.cts | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/routes/user.cts b/routes/user.cts index a346160..04f5aac 100644 --- a/routes/user.cts +++ b/routes/user.cts @@ -8,7 +8,16 @@ const { getDb } = require('../db/database.cts'); const { seedDemoData } = require('../scripts/seedDemoData.cts'); const { demoDataLimiter } = require('../middleware/rateLimiter.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 router.get('/seeded-status', (req: Req, res: Res) => { @@ -39,14 +48,7 @@ router.get('/seeded-status', (req: Req, res: Res) => { seededTransactions, }); } catch (err) { - res - .status(err.status || 500) - .json( - standardizeError( - err.message || 'Seeded status check failed', - err.code || 'SEEDED_STATUS_ERROR', - ), - ); + throw toUserOpError(err, 'SEEDED_STATUS_ERROR'); } }); @@ -66,14 +68,7 @@ router.post('/clear-demo-data', demoDataLimiter, (req: Req, res: Res) => { counts: result.counts, }); } catch (err) { - res - .status(err.status || 500) - .json( - standardizeError( - err.message || 'Clear demo data operation failed', - err.code || 'CLEAR_DEMO_ERROR', - ), - ); + throw toUserOpError(err, 'CLEAR_DEMO_ERROR'); } }); @@ -102,9 +97,7 @@ router.post('/seed-demo-data', demoDataLimiter, (req: Req, res: Res) => { counts: result.counts, }); } catch (err) { - res - .status(err.status || 500) - .json(standardizeError(err.message || 'Seed operation failed', err.code || 'SEED_ERROR')); + throw toUserOpError(err, 'SEED_ERROR'); } }); @@ -116,17 +109,13 @@ router.post('/erase-data', demoDataLimiter, (req: Req, res: Res) => { .trim() .toUpperCase(); if (confirm !== 'ERASE') { - return res - .status(400) - .json(standardizeError('Type ERASE to confirm.', 'ERASE_CONFIRM_REQUIRED', 'confirm')); + throw ValidationError('Type ERASE to confirm.', 'confirm', 'ERASE_CONFIRM_REQUIRED'); } try { const result = eraseUserData(getDb(), req.user.id); res.json({ success: true, ...result }); } catch (err) { - res - .status(err.status || 500) - .json(standardizeError(err.message || 'Failed to erase data', err.code || 'ERASE_ERROR')); + throw toUserOpError(err, 'ERASE_ERROR'); } });