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
Comments
This is just the message from JSON.parse, part of the JavaScript language. If you have any suggestions, I'm all ears :) |
I tagged this issue as help wanted because someone will need to do a PR to improve this. |
I was thinking try/catch with console.log with more information (HTTP method + path + body limited to 100 chars?). |
Oh, this is already 100% for you to do. I'm turning on my computer and I'll post you an example. |
Sure I'll do a PR. Is console.log acceptable in body-parser? Other logging solutions? |
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. |
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)
}) |
Sorry, modified the code above, because I accidentally put |
Great, thanks! |
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
|
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 :) |
Thanks a lot! |
Thaks a lot! too :) |
This doesn't help much in debugging the request. Is there a way to catch this and log a simple warning message?
The text was updated successfully, but these errors were encountered: