Open agussetyar opened 1 year ago
But, this behavior only happens in the Frontend side. I tested with Postman and the request looks fine.
@agussetyar I guess you need to add the header Accept: application/json
in your frontend request in order to receive a JSON response
But, this behavior only happens in the Frontend side. I tested with Postman and the request looks fine.
@agussetyar I guess you need to add the header
Accept: application/json
in your frontend request in order to receive a JSON response
Already tried this approach, but also not working in my cases. So far, I just return the default throw exception without any customization into JSON format.
fileFilter( req, bile, cb ) { cb( new Error( 'error' ), false ) }
Processing works fine on the web, but when an application uses network communication, a timeout occurs in the Pending state.
We have confirmed that the busboy.on('close') event does not fall into the Pending state.
I corrected the code as below so that the storage._handleFile handled the error.
/lib/make-middleware.js: 116
if (err) {
appender.removePlaceholder(placeholder)
return abortWithError(err)
}
if (!includeFile) {
appender.removePlaceholder(placeholder)
return fileStream.resume()
}
var aborting = false
pendingWrites.increment()
var aborting = false
pendingWrites.increment()
if (err||!includeFile) {
aborting = true
abortWithError(err)
}
I have encountered this issue as well. It can be replicated even when I use Postman. After using @Hongdaesik 's fix, it works now.
I think this issue is due to the empty array of uploadedFiles, which causes the remove callback to not be triggered.
@Hongdaesik I'm curious about why this bug block exactly the first request after the filter file error. before using your fix, the busboy status is _complete: false. But I'm not sure why busboy blocks other unrelated node js requests
original code: _complete: false, _hparser: null,
after applying the fix: _fileEndsLeft: 0, _fileStream: null, _complete: true,
busyboy _bparser before the fix:
for those who are waiting for merge, you can apply this fix in application code:
when there is a filefilter error, instead of passing it to the multer callback, you could flag it in req:
req.uploadError = new Error("filefilter error");
cb(null, false);
----
then handle it by using your customize middleware func,
const errorHandler = (req, res, next) => {
if (req.uploadError) {
return next(req.uploadError);
}
next();
};
const overriddenMulter = Object.keys(Object.getPrototypeOf(multerMiddleWare)).reduce(
(fncMapper, fncName) => {
if (typeof multerMiddleWare[fncName] !== "function") {
return fncMapper;
}
fncMapper[fncName] = (...key) => [
multerMiddleWare[fncName](...key),
errorHandler,
];
return fncMapper;
},
{}
);
e.g.
route.post("/", overriddenMulter.single("image"), yourcontrollerFunc);
@pwliuab I'll check it out in more detail and make further corrections when I have time! thx!
@Hongdaesik thanks, I will investigate the detail as well, seems interesting.
I'm working with very simple file upload feature using Express.js & Multer.
This is my multer middleware code:
This is my error handler middleware:
When I hit this endpoint from Frontend (React.js) using Axios and FormData. I got never ending 'pending' request and I dont know why this is happen with this simple code. The expected results is custom JSON response should be returned.
After a minutes searching, I found that if I remove the 'FileValidationError' from error handler logic or using this code:
The request is not pending and it returned response the Error() instance.
But, this behavior only happens in the Frontend side. I tested with Postman and the request looks fine.
Can someone tell me If I do something wrong in this situation?