Skip to content

[BUG]: Multer Upload Errors Not Handled β€” Server Returns Unformatted Crash ResponseΒ #7

Description

@Anushka-045

πŸ“Œ 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!

Metadata

Metadata

Assignees

Labels

NSOC'26bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions