-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
Description
I use multer for uploading files and if I upload an unexisting file or with a different file name then I get this
MulterError: Unexpected field
This is good that I catch the error but I can not send anymore request until I restart. Why ?
I use all of my files try catch and when I get an error I never got this problem. Only when it goes to my custom error handler:
export const app = express();
const upload = multer({
limits: {
fileSize: 10 * 1024 * 1024,
},
fileFilter: (_req, file, cb) => {
if (!allowedFileMimetypeAndExtensions.includes(file.mimetype)) {
return cb(new Error('Diese Bilddatei ist nicht erlaubt.'));
}
cb(null, true)
}
});
const redisStore = new RedisStorage({ client: redis });
// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());
// TracingHandler creates a trace for every incoming request
app.use(Sentry.Handlers.tracingHandler());
app.use(morgan('dev'));
app.use(express.urlencoded({extended: true}));
app.use(express.json());
//Reduce Fingerprint
app.disable('x-powered-by');
app.use(cors());
app.use(helmet());
app.post('/api/upload/images', [upload.array('image', 10)], async (req: Request, res: Response) => {
try {
console.log(req.file);
console.log(req.files);
return res.status(201).json({});
} catch(e) {
console.log(e);
Sentry.captureException(e);
return res.status(500).json({
message: e
});
}
});
// The error handler must be registered before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());
// Custom Error Handler HERE I CATCH THE ERROR
app.use(errorHandler);
app.listen(3000, () => {
console.log('SERVER LISTEN ON PORT 3000');
});
And here is my custom error handler
import * as Sentry from '@sentry/node';
import { NextFunction, Request, Response } from 'express';
export const errorHandler = (err: Error, _req: Request, res: Response, _next: NextFunction) => {
Sentry.captureException(err);
console.log(`Errorhandler: ${err}`);
return res.status(500).json({error: 'Etwas ist schief gelaufen.'});
};