hagopj13 / hagopj13/node-express-boilerplate

While using multer, erros crash server

Open
#228 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
2.3k
PR merge metrics
No merged PRs in 30d

Description

I have noticed that when using multer, the server crashes when you ```throw new ApiError()``` and I am pulling my hair out trying to track it down.

**Route**
```
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'src/data/mmsTemp/');
},
filename: (req, file, cb) => {
cb(null, Date.now() + '-' + file.originalname);
},
});

const upload = multer({ storage });
router
.route('/:api_key/messages/mms')
.get(apiKey, controllers.messages.getMMSMessages)
.post(
apiKey,
upload.single('file'),
controllers.messages.sendMMS
);
```

**Controller**
```
module.exports.sendMMS = async (req, res) => {
if (!req.file) {
throw new ApiError(httpStatus.BAD_REQUEST, 'Missing file')
}
const data = {
...req.body,
file: req.file,
apiKey: req.user.apiKey,
apiSecret: req.user.apiSecret,
domain: req.user.domain,
};
await cpaasService.messages.sendMMS(data)
return shSuccess(res);
};
```

Stack
```
error: Error: Missing file
at module.exports.sendMMS (C:\XXXXXXXX\src\controller\ucaas\messages.controller.js:44:11)
at Layer.handle [as handle_request] (C:\XXXXXXXX\node_modules\express\lib\router\layer.js:95:5)
at next (C:\XXXXXXXX\node_modules\express\lib\router\route.js:144:13)
at done (C:\XXXXXXXX\node_modules\multer\lib\make-middleware.js:45:7)
at indicateDone (C:\XXXXXXXX\node_modules\multer\lib\make-middleware.js:49:68)
at Multipart. (C:\XXXXXXXX\node_modules\multer\lib\make-middleware.js:166:7)
at Multipart.emit (node:events:527:28)
at Multipart.emit (node:domain:475:12)
at emitCloseNT (node:internal/streams/destroy:138:10)
at processTicksAndRejections (node:internal/process/task_queues:82:21)
info: Server closed
[nodemon] app crashed - waiting for file changes before starting...
```

Contributor guide

Open the contributing guide

Research direction

Start at src/controller/ucaas/messages.controller.js:44, where sendMMS throws ApiError, and trace the request through the multer upload.single('file') middleware shown in the issue. Reproduce the missing-file request and determine the expected error-handling path; done means the error is handled without crashing the server.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, javascript, node.js
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.