expressjs / body-parser

Node.js body parsing middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can not debug body on error

Fi1osof opened this issue · comments

I got an error:

SyntaxError: Unexpected string in JSON at position 147
    at JSON.parse (<anonymous>)
    at parse (/app/forward/node_modules/body-parser/lib/types/json.js:89:19)

I understand input request body is not valid, but i can not see why.
debug('parse json') does show nothing.

Can your add some more debug info to show body at least on exception?

Thanks!

If the body is available, it is provided in the body property on the error object https://github.com/expressjs/body-parser/blob/master/README.md#errors

If the body is available, it is provided in the body property on the error object https://github.com/expressjs/body-parser/blob/master/README.md#errors

It's hard to use cause should add error catching in each "use/post" operation. I use graphql-yoga which using bodyParser and can not catch error, cause error happens before i can add middleware. And i can't see body.
I create bodyParser fork and add body debug. This allow me see body and fix request error.

Show body in debug on exception in bodyParser may make my life easier.

Hi @Fi1osof thanks for the additional information. This module is not actually generating the logged message, and including it in the Errors message is generally not a great experience, as the body can contain new line characters, throwing off systems that are trying to parse error.stack, and can be extremely large and made of up user-defined data, tricking systems. An example is that a user could upload a specially-crafted body that would end up logging out what looks like multiple different exceptions when written to a log file.

Due to the complex issue around this, the body is not included in the Errors message by default and instead kept as a property on the error object. This is along the lines of how other errors are generated in Node.js core as well.

You may need to open an issue on the high-level system you are using to determine what a good method to get at the error object is and/or perhaps add a method (of flag) to see the body is the logging it generates.

@dougwilson thanks!
Yes, i create them: dotansimha/graphql-yoga#625
Hope will be fixed.