stream-utils / raw-body

Get and validate the raw body of a readable stream

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parse error

creeperyang opened this issue · comments

I want to test the length option but get Parse Error instead of the expected 400 error code. The lib cant capture the error and just throw it out.

rawBody(request, { limit: '1mb', encoding: 'utf8', length: 191 })

// the request:

request(app.listen())
    .post('/')
    .set('Content-Type', 'text/xml')
    .set('Content-Length', 191) // here is the length 
    .send(sourceXml) // the data's actual length is 197
    .expect(200, done);

and I get the error:

Error: Parse Error
      at Error (native)
      at Socket.socketOnData (_http_server.js:343:22)
      at Socket.emit (events.js:107:17)
      at readableAddChunk (_stream_readable.js:163:16)
      at Socket.Readable.push (_stream_readable.js:126:10)
      at TCP.onread (net.js:538:20)

Hi! I'll take a look into it, but for the cases where the length is longer than Content-Length and you are handling that in a Node.js code HTTP server, the Parse Error is probably too low for us to have any information that will be useful for you.

While I'm taking a look, please feel free to see if you can propose a pull request with a fix in the meantime as well!

Thanks for reply. I looked through the code and set some breakpoint, and I found maybe it's really too low to capture the error or control the handling flow.

So, I took a hard look into this just now, and I was not able to get the issue to be captured by this module. The provided error will pretty much always occur outside the scope of a request or response object, instead the parse error occurs on the socket. This module should raise a request aborted internally, but Node.js will destroy the socket on a parse error, so you wouldn't be able to respond with any HTTP response anyhow :/