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.

Difficult to determine that an error is the result of bad JSON

See original GitHub issue

Having SyntaxErrors consistently returned (#119) and a status code provided means we can fairly reliably detect errors in express middleware that are the result of bad JSON.

app.use(function(err, req, res, next) {
  if (err instanceof SyntaxError && err.status === 400) {
    console.error('Bad JSON');
  }
});

This test could theoretically give false positives though, if some other middleware were to throw something similar. Imagine we’re using body-parser first and then something else to validate the body has the structure we’re expecting, for example.

Is there any way to detect JSON parsing error with 100% reliability with the library as it is now? Is there something you could (perhaps in 2.0) to make this a little easier?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:6
  • Comments:26 (9 by maintainers)

github_iconTop GitHub Comments

18reactions
dougwilsoncommented, Sep 8, 2017

All errors include a type property from 1.18.0 release onwards. For parse failure, err.type === 'entity.parse.failed'.

7reactions
carlbennettnzcommented, Sep 7, 2015

That’s fine, I trust your judgement 😃

I’ll stick with

app.use(function(err, req, res, next) {
  if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
    console.error('Bad JSON');
  }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Most efficient way to fix an invalid JSON - Stack Overflow
It needs to be converted to valid JSON first. ... var a = new JavaScriptSerializer(); Dictionary<string, object> results = a.
Read more >
How to Fix the Invalid JSON Response Error in WordPress
Check your WordPress site URL. ... Another common cause of the invalid JSON response error is a problem with your WordPress site URLs....
Read more >
How to Fix the Invalid JSON Error in WordPress? - Rank Math
In this article, learn how to fix the invalid JSON error - 'Updating failed. The response is not a valid JSON response.' with...
Read more >
Solved: Find event with invalid JSON - Splunk Community
Trying to find a consistent way of finding events that contain invalid JSON. We've ran into all sorts of different issues with escape...
Read more >
SyntaxError: JSON.parse: bad parsing Breaking Your Code ...
Traveling deftly through to the next item in our JavaScript Error Handling series, today we're taking a hard look at the JSON Parse...
Read more >

github_iconTop Related Medium Post

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