Open pullmann4rent opened 8 months ago
@pullmann4rent I don't see anything obvious in the code. Multer calls the callback with an Error object can you see in your code which callback is getting that error object?
"I'm facing the same issue. It happens when the multipart/form-data field contains an expected object with a different name. If I set it as 'file' and send it as 'item,' it will respond that it doesn't exist. The problem is that it sends a 500 error to the server and gets stuck in a loop. If I use multer's handleError, the error will be caught only once and the entire system will be stuck until a restart is necessary."
Can we get a test case to reproduce?
To fix the problem, I had to change upload.array to upload.any and do the validations internally
1.) The upload must be a multer.array('name', X). X can be any number
2.) In the request, enter a name other than "name". It will give the multer error, but it gets stuck in the infinite loop. Ex: field "image" to "images"
In other words, if I'm creating an api and the user uses a name other than the one required, the multer triggers the error and gets stuck. The solution is to restart the application.
How does the user submit a different name other than the one required?
I was making an image upload API for AWS S3 that accepts an array of fields of type 'multipart/form-data'.
However, in my request I expect the field to be file, that is, a file array.
If the user makes a mistake in the field and sends files, the multer gives an error 500 and warns "Unexpected field". However, I don't want it to give a 500 error, but rather another custom error.
If I use multer's handleError, I can catch the exception and send a custom error, but it stays in an infinite loop until the application crashes
Is the user typing in a textbox the word "files"? Is this test case the same scenario as yours?
Example
Expectative: file User: User change to files, error 500 looping
Thank you for the additional information. It looks like there is a unit test for the unexpected field name situation.
My current guess is that the "looping" behavior is caused by additional code and how it interacts with the request/response handling code. Would you be able to post the code that's handling errors?
Inside my try cath there is my upload logic.
app.post('/upload-files', upload.array('file', 6), handleMulterError, async (req: Request, res: Response, next: NextFunction) => {
try {
.....
} catch (error) {
.....
}
});
function handleMulterError(err: any, req: Request, res: Response, next: NextFunction) {
if (err instanceof multer.MulterError) {
if (err.code === 'LIMIT_UNEXPECTED_FILE') {
return res.status(403).send({
success: false,
error: 'No files uploaded',
});
}
}
next(err);
}
The problem is when the multer error occurs. I can give the 403 error, but it will be stuck in a loop waiting for request. I've already tried removing next(err) and it gives the same error.
To solve the problem, I replaced the array with any, and inside I did the logic of going through the array and seeing if it was the correct field, giving my custom error without crashing the app
Can you try "using" the error handler after the app.post
handler registration? Something like this test.
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:
And here is my custom error handler