EverJay Dihenkar
EverJay Dihenkar

Reputation: 143

I'm getting a ParseError in nodejs and with a really not proper error message

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: Parse Error
    at Error (native)
    at Socket.socketOnData (_http_client.js:317:20)
    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)

What can be the possible cause of this error? And where can I put the try-catch or fun(err) for error handling! I'm even not getting the source of this error, what can be the possible source of this error?

Upvotes: 4

Views: 12661

Answers (3)

skypjack
skypjack

Reputation: 50550

It looks like you are not listening to errors at all. Try as it follows:

http.get(url, function(res) {
    res.on('error', function(err) {
        // here you can handle errors on the response
    });
}).on('error', function(err) {
    // here you can handle errors on the request
});

This way you can catch the error and try to understand what's happening. Nobody can say why you are receiving a parse error with the data provided till now.

Upvotes: 2

David Wickstrom
David Wickstrom

Reputation: 5198

I got a very similar stack trace when running gulp as a proxy.

Error: Parse Error
    at Socket.socketOnData (_http_client.js:411:20)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:191:7)
    at readableAddChunk (_stream_readable.js:178:18)
    at Socket.Readable.push (_stream_readable.js:136:10)
    at TCP.onread (net.js:563:20)

I found the root cause after a while.

The backend server was sending duplicate headers due to a bug in the backend. By removing the duplicate HTTP header This error in the javascript frontend was fixed.

Example of duplicate http headers (for example content-length):

< HTTP/1.1 200 OK
< server: Cowboy
< date: Thu, 25 Jan 2018 12:47:15 GMT
< content-length: 4789
< content-type: text/html
< content-length: 4789
< date: Thu, 25 Jan 2018 12:47:14 GMT
< server: Cowboy

Upvotes: 3

robertklep
robertklep

Reputation: 203419

Without any code I can only guess to the specific reason, but it looks like you're trying to make an HTTP request to a server that is returning an invalid HTTP response.

To catch these errors, use an error event handler on your client (provided that you're using the http module):

var http = require('http');

http.get(URL, function(response) { ... }).on('error', function(err) {
  console.error(err); // or however you want to deal with it
});

Upvotes: 2

Related Questions