π Problem
The multer middleware in admin.routes.js throws errors when an invalid file type is uploaded, but these errors are never caught by an error handler. This causes the server to return an unformatted crash response instead of a clean JSON error message, which is bad for UX and security.
π‘ Proposed Solution
Add a dedicated multer error handler middleware in admin.routes.js that:
- Catches MulterError (e.g. file too large)
- Catches custom fileFilter errors (wrong file type)
- Returns a clean JSON response with a proper status code and message
π― Benefits
- Prevents server crashes from invalid file types
- Improves security by blocking unexpected uploads
- Gives users a clear error message instead of a crash
βοΈ Implementation
Add this after the upload route:
router.use((err, req, res, next) => {
if (err instanceof multer.MulterError || err.message) {
return res.status(400).json({
status: 'error',
message: err.message
});
}
next(err);
});
I'd like to work on this!
π Problem
The multer middleware in admin.routes.js throws errors when an invalid file type is uploaded, but these errors are never caught by an error handler. This causes the server to return an unformatted crash response instead of a clean JSON error message, which is bad for UX and security.
π‘ Proposed Solution
Add a dedicated multer error handler middleware in admin.routes.js that:
π― Benefits
βοΈ Implementation
Add this after the upload route:
router.use((err, req, res, next) => {
if (err instanceof multer.MulterError || err.message) {
return res.status(400).json({
status: 'error',
message: err.message
});
}
next(err);
});
I'd like to work on this!