expressjs / expressjs/multer

Multer doesn't seem to be forwarding error to Express

Open
#904 4 comments 4 reactions 0 assignees View on GitHub
discuss question
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.