-
Notifications
You must be signed in to change notification settings - Fork 7.3k
http.ClientResponse should have an error event #3375
Comments
ClientResponse does emit an error event in this case, doesn't it? var req = http.request(options, function (res) {
res.on('error', handler)
}) |
The documentation doesn't show an error event in the response object: http://clipboard.com/clip/LQhHMG1qw4PaDdSPeTPKaYCzHQMELRCijRLe I added one just incase but it didn't get raised. For example on this head request: ~$ curl -I "http://pressebo.ivwbox.de/cgi-bin/ivw/CP/MainIndex;?r=&d=11727.789742872119" The content length is wrong, resulting in a Parse Error which only seems to be able to be caught if you hook up to the socket error. |
Ah - I see ClientResponse implements ReadableStream that does have an error event. However it looks like if you set the options.method to HEAD then the error is not raised on the ClientResponse object. Here is a repro:
OUTPUT: events.js:48 |
Ah. Yes, that's a bug. Confirmed. |
Confirmation test: var http = require('http');
var net = require('net');
var server = net.createServer(function(sock) {
sock.end(
'HTTP/1.1 200 OK\r\n' +
'Date: ' + new Date().toUTCString() + '\r\n' +
'Content-Length: 1\r\n' + // wrong length
'Content-Type: text/plain\r\n' +
'\r\n' +
'\r\n' +
'This is too long.\r\n' +
'\r\n' +
'\r\n');
sock.destroySoon();
server.close();
});
server.listen(1337);
var options = { host: 'localhost',
port: 1337,
path: '/',
method: 'HEAD' };
var req = http.request(options, function (res) {
req.hadResponse = true;
res.hadResponse = true;
console.error('response:', res.statusCode, res.headers);
res.on('error', function(error) {
console.error('res error', error);
});
req.on('error', function(error) {
console.error('req error', error);
});
res.socket.on('error', function(error) {
console.error('socket error', error);
});
});
req.end(); Expect: response error. |
Refs #3354.
Node (http-parser) cannot detect it. See section "4.4 Message Length" of RFC2616. Then, Node detects invalid data after the response body. (i.e. The response is completed definitely.) |
Closed by #3777 |
Currently if there is an error parsing the response from a server e.g. the server sends back an incorrect value in the content length, this will result in an unhandledException unless you add the following code:
There should just be an 'error' event on the http.ClientResponse object that you can easily listen to in order to catch the error instead of having to go down to the socket level and hook up there.
The text was updated successfully, but these errors were encountered: