Multer doesn't seem to be forwarding error to Express
- Dominant language
- JavaScript
- Stars
- 12.1k
- Forks
- 1.1k
- Avg merge
- 8d 2h
- Merged PRs (30d)
- 21
Description
I'm using multer version 1.4.2. According to https://github.com/expressjs/multer#error-handling, multer should automatically forward the error to Express. I implemented a default error handler in express like this:
```
app.use((err, req, res, next) => {
let error = err.message;
if (err instanceof BadRequest) {
res.status(400);
} else if (err instanceof NotFound) {
res.status(404);
} else if (err instanceof NotAuthorized) {
res.status(401);
} else {
res.status(500);
console.error(err.stack);
error = `Server-side error`;
}
res.send({
error: error
})
})
```
This handler is correctly invoked by my own logic when I throw an error. I initialized multer as follows:
```
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads')
},
filename: (req, file, cb) => {
cb(null, generateFilename(req.query));
}
});
```
My method `generateFilename` throws an error when query is not in proper format. Instead of being forwarded to my default error handler in Express, however, the error causes the entire server to crash from uncaught exception.
Contributor guide
Assessment
This issue has not been assessed yet.