When a request is thrown to my application, I send a request to another webserver. But at times, I receive some weird parse error I've never seen before.
Here is how the parse error looks like:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Parse Error
at Socket.ondata (http.js:1150:24)
at TCP.onread (net.js:354:27)
This is the code that I use to send the request:
var request = http.request(options);
request.end();
request.on('response', function (response) {
response.on('end', function () {
console.log('Response received');
}
}
The question that I have is how can I mitigate this so I can throw the request again when this error occurs?
first of all, you should be putting all your .on() listeners before you .end() in general since not all APIs nextTick their .end() method.
second of all, node's event emitters throw errors if you do not have a .on('error') listener. so your code should look more like this
var request = http.request(options)
request.on('response', function (response) {
response.on('end', function () {
console.log('Response received.')
})
response.on('error', function (err) {
console.log('I got a response error.')
console.error(err.stack)
})
})
request.on('error', function (err) {
console.log('I got a request error.')
console.error(err.stack)
})
request.end()