Giter Site home page Giter Site logo

Better error logging about body-parser HOT 13 CLOSED

expressjs avatar expressjs commented on March 28, 2024
Better error logging

from body-parser.

Comments (13)

dougwilson avatar dougwilson commented on March 28, 2024

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.

dougwilson avatar dougwilson commented on March 28, 2024

I tagged this issue as help wanted because someone will need to do a PR to improve this.

from body-parser.

analytically avatar analytically commented on March 28, 2024

I was thinking try/catch with console.log with more information (HTTP method + path + body limited to 100 chars?).

from body-parser.

dougwilson avatar dougwilson commented on March 28, 2024

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.

analytically avatar analytically commented on March 28, 2024

Sure I'll do a PR. Is console.log acceptable in body-parser? Other logging solutions?

from body-parser.

dougwilson avatar dougwilson commented on March 28, 2024

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.

dougwilson avatar dougwilson commented on March 28, 2024

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.

dougwilson avatar dougwilson commented on March 28, 2024

Sorry, modified the code above, because I accidentally put req.body instead of err.body.

from body-parser.

analytically avatar analytically commented on March 28, 2024

Great, thanks!

from body-parser.

dougwilson avatar dougwilson commented on March 28, 2024

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.

dougwilson avatar dougwilson commented on March 28, 2024

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.

analytically avatar analytically commented on March 28, 2024

Thanks a lot!

from body-parser.

JuanDelgadillo avatar JuanDelgadillo commented on March 28, 2024

Thaks a lot! too :)

from body-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.