Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error logging #85

Closed
analytically opened this issue Mar 15, 2015 · 13 comments
Closed

Better error logging #85

analytically opened this issue Mar 15, 2015 · 13 comments
Assignees
Labels

Comments

@analytically
Copy link

SyntaxError: Unexpected token $
  at Object.parse (native)
  at parse (/etc/acme/webauth/node_modules/body-parser/lib/types/json.js:84:17)
  at /etc/acme/webauth/node_modules/body-parser/lib/read.js:102:18
  at IncomingMessage.onEnd (/etc/acme/webauth/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:944:16
  at process._tickDomainCallback (node.js:486:13)

This doesn't help much in debugging the request. Is there a way to catch this and log a simple warning message?

@dougwilson
Copy link
Contributor

This is just the message from JSON.parse, part of the JavaScript language. If you have any suggestions, I'm all ears :)

@dougwilson
Copy link
Contributor

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

@analytically
Copy link
Author

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

@dougwilson
Copy link
Contributor

Oh, this is already 100% for you to do. I'm turning on my computer and I'll post you an example.

@dougwilson dougwilson self-assigned this Mar 15, 2015
@analytically
Copy link
Author

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

@dougwilson
Copy link
Contributor

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.

@dougwilson
Copy link
Contributor

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)
})

@dougwilson
Copy link
Contributor

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

@analytically
Copy link
Author

Great, thanks!

@dougwilson
Copy link
Contributor

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)

@dougwilson
Copy link
Contributor

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 :)

@analytically
Copy link
Author

Thanks a lot!

@JuanDelgadillo
Copy link

Thaks a lot! too :)

@expressjs expressjs locked and limited conversation to collaborators Nov 19, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants