unit test fixes + error refactor
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 10m20s

This commit is contained in:
2025-12-11 14:16:25 -08:00
parent d6f0b446a5
commit 5f1901b93d
28 changed files with 704 additions and 260 deletions

View File

@@ -138,7 +138,9 @@ router.put('/corrections/:id', async (req, res, next: NextFunction) => {
res.status(200).json(updatedCorrection);
} catch (error) {
if (error instanceof Error && error.message.includes('not found')) {
return res.status(404).json({ message: error.message });
const err = new Error(error.message);
(err as any).status = 404;
return next(err);
}
next(error);
}
@@ -156,7 +158,9 @@ router.put('/recipes/:id/status', async (req, res, next: NextFunction) => {
res.status(200).json(updatedRecipe);
} catch (error) {
if (error instanceof Error && error.message.includes('not found')) {
return res.status(404).json({ message: error.message });
const err = new Error(error.message);
(err as any).status = 404;
return next(err);
}
next(error);
}
@@ -205,7 +209,9 @@ router.delete('/recipes/:recipeId', async (req, res, next: NextFunction) => {
res.status(204).send();
} catch (error) {
if (error instanceof Error && error.message.includes('not found')) {
return res.status(404).json({ message: error.message });
const err = new Error(error.message);
(err as any).status = 404;
return next(err);
}
next(error);
}
@@ -226,7 +232,9 @@ router.delete('/flyers/:flyerId', async (req, res, next: NextFunction) => {
res.status(204).send();
} catch (error) {
if (error instanceof Error && error.message.includes('not found')) {
return res.status(404).json({ message: error.message });
const err = new Error(error.message);
(err as any).status = 404;
return next(err);
}
next(error);
}
@@ -244,7 +252,9 @@ router.put('/comments/:id/status', async (req, res, next: NextFunction) => {
res.status(200).json(updatedComment);
} catch (error) {
if (error instanceof Error && error.message.includes('not found')) {
return res.status(404).json({ message: error.message });
const err = new Error(error.message);
(err as any).status = 404;
return next(err);
}
next(error);
}
@@ -292,11 +302,6 @@ router.put('/users/:id', async (req, res, next: NextFunction) => {
const updatedUser = await db.adminRepo.updateUserRole(req.params.id, role);
res.json(updatedUser);
} catch (error) {
if (error instanceof ForeignKeyConstraintError) { // This error is thrown by the DB layer
return res.status(404).json({ message: `User with ID ${req.params.id} not found.` });
} else if (error instanceof Error && error.message.includes('not found')) { // This handles the generic "not found" from the repo
return res.status(404).json({ message: error.message });
}
logger.error(`Error updating user ${req.params.id}:`, { error });
next(error);
}
@@ -513,25 +518,4 @@ router.post('/trigger/weekly-analytics', async (req, res, next: NextFunction) =>
}
});
/**
* Admin-specific error handling middleware.
* This should be the last middleware added to this router.
* It catches any errors passed by `next(error)` in the routes above.
*/
router.use((error: Error, req: Request, res: Response, next: NextFunction) => {
// Generate a unique ID for this specific error occurrence.
const errorId = crypto.randomBytes(4).toString('hex');
// Log the full error details on the server for debugging.
logger.error(`[API /admin] Error ID: ${errorId} - An unhandled error occurred in the admin router.`, {
error: error.message,
stack: error.stack,
url: req.originalUrl,
method: req.method,
});
// Send a generic, safe response to the client.
res.status(500).json({ message: `An internal server error occurred. Please try again later. If the problem persists, please reference error ID: ${errorId}` });
});
export default router;