question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Requests can hang with malformed JSONs; depends on error handling

See original GitHub issue

Jayden,

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:

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:

  1. Send a malformed request (e.q. an invalid operations JSON).
  2. 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:

  1. Use response.end() instead of .send() or .json(). I’d like to stick with .json, however.
  2. Manually emit the end event in case of an error: request.emit("end").

Questions to you:

  1. Do I understand your code correctly?
  2. Do you know why the end event is not firing in case of an error in the upload middleware?
  3. Does emitting end manually seem like a good fix which can be applied now?
  4. 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:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jaydensericcommented, Nov 17, 2018

The fix is published in graphql-upload@8.0.2, thanks all 🙌

1reaction
juonacommented, Nov 16, 2018

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 ^^

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling for invalid JSON submitted - Treehouse
Error handling for invalid JSON submitted. When I POST invalid JSON I don't get the usual error messages, neither 400 nor 500, how...
Read more >
Handling bad JSON.parse() in node safely - Stack Overflow
The best way to catch invalid JSON parsing errors is to put the calls to JSON.parse() to a try/catch block. You really do...
Read more >
Guide to Spring Boot REST API Error Handling - Toptal
The Spring Boot default answer, without proper error handling, looks like this: ... the error message will be “Malformed JSON request” and the...
Read more >
Receiving a Malformed JSON Error When Using API Doc
Cause. The malformed JSON error means that the format of the API call is incorrect.
Read more >
A Comprehensive Guide To Error Handling In Node.js
Errors happen in every application. Devs have to decide: do you write code to handle the error? Suppress it? Notify the user?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found