Requests can hang with malformed JSONs; depends on error handling
See original GitHub issueJayden,
Great work on the library, and thanks for pushing guys @ Apollo. Your workaround works well enough so that I wouldn’t care about them fixing their own dependency.
I am now using graphql-upload@8.0.1 with Express and have found this one rather small issue.
In the express middleware you have written code which (please correct me if I am wrong) is meant to delay the final response until the upload has ended:
-
https://github.com/jaydenseric/graphql-upload/blob/master/src/graphqlUploadExpress.mjs#L36
-
https://github.com/jaydenseric/graphql-upload/blob/master/src/graphqlUploadExpress.mjs#L32.
The issue is that the finished
promise is listening for request.on("end")
, which apparently doesn’t happen if an error occurs while uploading. Scenario is this:
- Send a malformed request (e.q. an invalid
operations
JSON). - Have an error handler, which uses
request.send
(or.json
- it resolves to.send
). E.g. I have this little piece:
app.use((err, _, res, __) => {
res.status(err.status).json({
message: httpStatus[err.status],
stack: config.printErrorStack ? err.stack : {}
});
});
When .json()
is called, the flow goes to your middleware:
https://github.com/jaydenseric/graphql-upload/blob/master/src/graphqlUploadExpress.mjs#L36
However, in this scenario the end
event is never fired on the request
. Two workarounds are available:
- Use
response.end()
instead of.send()
or.json()
. I’d like to stick with.json
, however. - Manually emit the
end
event in case of an error:request.emit("end")
.
Questions to you:
- Do I understand your code correctly?
- Do you know why the
end
event is not firing in case of an error in the upload middleware? - Does emitting
end
manually seem like a good fix which can be applied now? - Would you fix this? I guess you could do this using processRequest.catch() but I am not sure what your desired outcome would be in this case.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
The fix is published in
graphql-upload@8.0.2
, thanks all 🙌Thank you, the explanation makes sense : ]
Come to think of it, I did notice that smaller JPEGs seemed to work differently, i.e. would sometimes return responses, but the behaviour was inconsistent and I couldn’t understand what was going on, so I didn’t mention it in the description.
Happy to see smart people working on packages that I use - makes me feel secure ^^