Comments (13)
This is just the message from JSON.parse, part of the JavaScript language. If you have any suggestions, I'm all ears :)
from body-parser.
I tagged this issue as help wanted because someone will need to do a PR to improve this.
from body-parser.
I was thinking try/catch with console.log with more information (HTTP method + path + body limited to 100 chars?).
from body-parser.
Oh, this is already 100% for you to do. I'm turning on my computer and I'll post you an example.
from body-parser.
Sure I'll do a PR. Is console.log acceptable in body-parser? Other logging solutions?
from body-parser.
No, this is something you simply implement yourself in your own code as part of Express error handling, nothing you need to change in this module. I mis-understood your original request.
from body-parser.
Simply add this to your application:
app.use(function logJsonParseError(err, req, res, next) {
if (err.status === 400 && err.name === 'SyntaxError' && err.body) {
// Display extra information for JSON parses
console.log('JSON body parser error!')
console.log(req.method + ' ' + req.url)
console.log(err.body.slice(0, 100).toString())
}
next(err)
})
from body-parser.
Sorry, modified the code above, because I accidentally put req.body
instead of err.body
.
from body-parser.
Great, thanks!
from body-parser.
This is what the output looks like :) Feel free to tweak as needed. Basically the main thing to note here is that in this module, when we have a parse error, we put the full decoded body in the body
property on the err
object so you can inspect it later :)
JSON body parser error!
PUT /
{"bob":$foo}
SyntaxError: Unexpected token $
at Object.parse (native)
at parse (node_modules\body-parser\lib\types\json.js:84:17)
at node_modules\body-parser\lib\read.js:102:18
at IncomingMessage.onEnd (node_modules\body-parser\node_modules\raw-body\index.js:136:7)
at IncomingMessage.g (events.js:180:16)
at IncomingMessage.emit (events.js:92:17)
at _stream_readable.js:943:16
at process._tickCallback (node.js:419:13)
from body-parser.
And, if you cannot get it to work, please feel free to post back here and I'm happy to help :) And I'm of course sorry again for misunderstanding the question initially :)
from body-parser.
Thanks a lot!
from body-parser.
Thaks a lot! too :)
from body-parser.
Related Issues (20)
- HTTP:413 Request Entity Too Large HOT 5
- Conform with express-validator on parameter representation
- bodyParser is deprecated, error HOT 1
- bodyparser.json() shown as deprecated? (question) HOT 7
- pass options to qs thru urlencoded? HOT 4
- Cannot catch SyntaxError when user provides invalid JSON in body and content-type: application/json HOT 3
- Support for content-encoding: deflate raw HOT 7
- req.body could not be accessed when send as form data, but works fine with JSON HOT 3
- Pass custom parameters to `qs` HOT 3
- Update iconv-lite to latest 0.6.3 HOT 7
- support for ndjson
- Regular Expression Denial of Service (ReDoS) in [email protected] HOT 3
- Update `debug` dependency (memory leak leading to vulnerability) HOT 1
- api calls made with invalid json HOT 5
- Add support for removing body parser limit HOT 8
- BadRequestError: request size did not match content length HOT 7
- How to handle content-type mismatch? HOT 1
- CVE-2017-20165 - debug HOT 2
- Issue HOT 1
- Debug package version in body parser showing security vulnerability HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from body-parser.